Here you can find the source of quotePattern(String pattern)
Parameter | Description |
---|---|
pattern | pattern to quote |
public static String quotePattern(String pattern)
//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; } }