Here you can find the source of toRegexPattern(String pattern)
Parameter | Description |
---|---|
pattern | a pattern string |
private static String toRegexPattern(String pattern)
//package com.java2s; /******************************************************************************* * Copyright (c) 2007, 2008 Anyware Technologies 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 * //from w ww . jav a 2 s. c om * ModelSearchQueryTextualExpressionMatchingHelper.java * * Contributors: * Lucas Bigeardel (Anyware Technologies) - initial API and implementation * Lucas Bigeardel - added partial matching strategy ******************************************************************************/ public class Main { /** * Converts a pattern string with '*' and '?' into a regular expression * pattern string. * * @param pattern * a pattern string * @return a regular expression pattern string */ private static String toRegexPattern(String pattern) { int patternLength = pattern.length(); StringBuffer result = new StringBuffer(patternLength); boolean escaped = false; boolean quoting = false; for (int i = 0; i < patternLength; i++) { char ch = pattern.charAt(i); switch (ch) { case '*': { if (!escaped) { if (quoting) { result.append("\\E"); //$NON-NLS-1$ quoting = false; } } result.append(".*"); //$NON-NLS-1$ escaped = false; break; } case '?': { if (!escaped) { if (quoting) { result.append("\\E"); //$NON-NLS-1$ quoting = false; } } result.append("."); //$NON-NLS-1$ escaped = false; break; } case '\\': { if (!escaped) { escaped = true; } else { escaped = false; if (quoting) { result.append("\\E"); //$NON-NLS-1$ quoting = false; } result.append("\\\\"); //$NON-NLS-1$ } break; } default: { if (!quoting) { result.append("\\Q"); //$NON-NLS-1$ quoting = true; } if (escaped && ch != '*' && ch != '?' && ch != '\\') { result.append('\\'); } result.append(ch); escaped = (ch == '\\'); } } } if (quoting) { result.append("\\E"); //$NON-NLS-1$ } return result.toString(); } }