public static String escape ( String literal ) { StringBuilder sb = new StringBuilder ( ) ; for ( int ii = 0 ; ii < literal . length ( ) ; ii ++ ) { char cc = literal . charAt ( ii ) ; switch ( cc ) { case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : sb . append ( "\\" ) . append ( cc ) ; break ; default : sb . append ( cc ) ; break ; } } return sb . toString ( ) ; }
Escapes all regex control characters returning expression suitable for literal parsing .