public static String javaUtilRegexEscape(String s){
  if (s.indexOf("\\E") == -1) {
    return "\\Q" + s + "\\E";
  }
  StringBuilder sb=new StringBuilder();
  for (int i=0; i < s.length(); i++) {
    sb.append('\\');
    sb.append(s.charAt(i));
  }
  return sb.toString();
}

public static String javaUtilRegexEscape ( String s ) { if ( s . indexOf ( "\\E" ) == - 1 ) { return "\\Q" + s + "\\E" ; } StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { sb . append ( '\\' ) ; sb . append ( s . charAt ( i ) ) ; } return sb . toString ( ) ; }
escapes the special characters from a string so it can be used as part of a regex pattern . this method is for use on regexes in the flavor of the java . util . regex package . this method should be removed when we move to the java version 1 . 5 ( tiger ) release , since that release gives us a literal regex flag as well as a quote method to produce literal regexes .
