Java String Quote quotePattern(String pattern)

Here you can find the source of quotePattern(String pattern)

Description

Quotes a string pattern as non-regular expression string.

License

Open Source License

Parameter

Parameter Description
pattern pattern to quote

Return

the quoted pattern

Declaration

public static String quotePattern(String pattern) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  w  w.  j a  v  a 2 s  .  c  om*/
 *     IBM Corporation - initial API and implementation
 *     Bredex GmbH - Creator of this class.
 ******************************************************************************/

public class Main {
    /**
     * Quotes a string pattern as non-regular expression string.
     * That means: no regular expresion instructions in the given string 
     * won't be taken into account.<br/>
     * Example:
     * <code>String searchString = "xy[^a]";<br/>
     * Pattern.compile(quotePattern(searchString)).matcher(searchString)
     *       .matches();
     * <br/> </code> will return true.
     * @param pattern pattern to quote
     * @return the quoted pattern
     */
    public static String quotePattern(String pattern) {
        final String START = "\\Q"; //$NON-NLS-1$
        final String STOP = "\\E"; //$NON-NLS-1$
        final int STOP_LENGTH = 2; // STOP.length()

        int stopIndex = pattern.indexOf(STOP);
        if (stopIndex < 0) {
            return START + pattern + STOP;
        }

        String result = START;
        for (int position = 0;;) {
            stopIndex = pattern.indexOf(STOP, position);
            if (stopIndex >= 0) {
                result += pattern.substring(position, stopIndex + 2) + "\\" + STOP + START; //$NON-NLS-1$
                position = stopIndex + STOP_LENGTH;
            } else {
                result += pattern.substring(position) + STOP;
                break;
            }
        }

        return result;
    }
}

Related

  1. quotEncode(String txt)
  2. quotEncode(String txt)
  3. quoteOutputName(String name)
  4. quoteParameter(String param)
  5. quotePath(String path)
  6. quotePattern(String s)
  7. quotePlainText(final String textContent)
  8. quoteQuery(String query)
  9. quoteReference(final String reference)