@Test public void testEscapeSomeRegexCharacters() { char[] toIgnore = { '(', ')' }; String escaped = UserSearchUtils.escapeSomeRegexCharacters("start^.$()[]+&{}*?end", toIgnore); assertEquals("start\\^\\.\\$()\\[\\]\\+\\&\\{\\}\\*\\?end", escaped); toIgnore = new char[] { '^', '*', '?' }; escaped = UserSearchUtils.escapeSomeRegexCharacters("start^.$()[]+&{}*?end", toIgnore); assertEquals("start^\\.\\$\\(\\)\\[\\]\\+\\&\\{\\}*?end", escaped); }
static String escapeSomeRegexCharacters(String input, char[] doNotEscape) { StringBuffer buffy = new StringBuffer(); for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (contains(doNotEscape, c)) { buffy.append(c); continue; } switch (c) { case '^': buffy.append("\\^"); break; case '.': buffy.append("\\."); break; case '$': buffy.append("\\$"); break; case '(': buffy.append("\\("); break; case ')': buffy.append("\\)"); break; case '[': buffy.append("\\["); break; case ']': buffy.append("\\]"); break; case '+': buffy.append("\\+"); break; case '&': buffy.append("\\&"); break; case '{': buffy.append("\\{"); break; case '}': buffy.append("\\}"); break; case '*': buffy.append("\\*"); break; case '?': buffy.append("\\?"); break; case '|': buffy.append("\\|"); break; default: buffy.append(c); break; } } return buffy.toString(); }
UserSearchUtils { static String escapeSomeRegexCharacters(String input, char[] doNotEscape) { StringBuffer buffy = new StringBuffer(); for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (contains(doNotEscape, c)) { buffy.append(c); continue; } switch (c) { case '^': buffy.append("\\^"); break; case '.': buffy.append("\\."); break; case '$': buffy.append("\\$"); break; case '(': buffy.append("\\("); break; case ')': buffy.append("\\)"); break; case '[': buffy.append("\\["); break; case ']': buffy.append("\\]"); break; case '+': buffy.append("\\+"); break; case '&': buffy.append("\\&"); break; case '{': buffy.append("\\{"); break; case '}': buffy.append("\\}"); break; case '*': buffy.append("\\*"); break; case '?': buffy.append("\\?"); break; case '|': buffy.append("\\|"); break; default: buffy.append(c); break; } } return buffy.toString(); } }
UserSearchUtils { static String escapeSomeRegexCharacters(String input, char[] doNotEscape) { StringBuffer buffy = new StringBuffer(); for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (contains(doNotEscape, c)) { buffy.append(c); continue; } switch (c) { case '^': buffy.append("\\^"); break; case '.': buffy.append("\\."); break; case '$': buffy.append("\\$"); break; case '(': buffy.append("\\("); break; case ')': buffy.append("\\)"); break; case '[': buffy.append("\\["); break; case ']': buffy.append("\\]"); break; case '+': buffy.append("\\+"); break; case '&': buffy.append("\\&"); break; case '{': buffy.append("\\{"); break; case '}': buffy.append("\\}"); break; case '*': buffy.append("\\*"); break; case '?': buffy.append("\\?"); break; case '|': buffy.append("\\|"); break; default: buffy.append(c); break; } } return buffy.toString(); }  }
UserSearchUtils { static String escapeSomeRegexCharacters(String input, char[] doNotEscape) { StringBuffer buffy = new StringBuffer(); for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (contains(doNotEscape, c)) { buffy.append(c); continue; } switch (c) { case '^': buffy.append("\\^"); break; case '.': buffy.append("\\."); break; case '$': buffy.append("\\$"); break; case '(': buffy.append("\\("); break; case ')': buffy.append("\\)"); break; case '[': buffy.append("\\["); break; case ']': buffy.append("\\]"); break; case '+': buffy.append("\\+"); break; case '&': buffy.append("\\&"); break; case '{': buffy.append("\\{"); break; case '}': buffy.append("\\}"); break; case '*': buffy.append("\\*"); break; case '?': buffy.append("\\?"); break; case '|': buffy.append("\\|"); break; default: buffy.append(c); break; } } return buffy.toString(); }  static Pattern createSearchPattern(String input, boolean caseSensitive); static Pattern createLiteralSearchPattern(String text); static Pattern createStartsWithPattern(String input, boolean allowGlobbing,
			int options); static Pattern createEndsWithPattern(String input, boolean allowGlobbing, int options); static Pattern createContainsPattern(String input, boolean allowGlobbing, int options); static Pattern createPattern(String input, boolean allowGlobbing, int options); static String createPatternString(String input, boolean allowGlobbing); }
UserSearchUtils { static String escapeSomeRegexCharacters(String input, char[] doNotEscape) { StringBuffer buffy = new StringBuffer(); for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (contains(doNotEscape, c)) { buffy.append(c); continue; } switch (c) { case '^': buffy.append("\\^"); break; case '.': buffy.append("\\."); break; case '$': buffy.append("\\$"); break; case '(': buffy.append("\\("); break; case ')': buffy.append("\\)"); break; case '[': buffy.append("\\["); break; case ']': buffy.append("\\]"); break; case '+': buffy.append("\\+"); break; case '&': buffy.append("\\&"); break; case '{': buffy.append("\\{"); break; case '}': buffy.append("\\}"); break; case '*': buffy.append("\\*"); break; case '?': buffy.append("\\?"); break; case '|': buffy.append("\\|"); break; default: buffy.append(c); break; } } return buffy.toString(); }  static Pattern createSearchPattern(String input, boolean caseSensitive); static Pattern createLiteralSearchPattern(String text); static Pattern createStartsWithPattern(String input, boolean allowGlobbing,
			int options); static Pattern createEndsWithPattern(String input, boolean allowGlobbing, int options); static Pattern createContainsPattern(String input, boolean allowGlobbing, int options); static Pattern createPattern(String input, boolean allowGlobbing, int options); static String createPatternString(String input, boolean allowGlobbing); final static String STAR; final static Pattern NON_GLOB_BACKSLASH_PATTERN; }