static String sqlToRegexSimilar ( String sqlPattern , char escapeChar ) { similarEscapeRuleChecking ( sqlPattern , escapeChar ) ; boolean insideCharacterEnumeration = false ; final StringBuilder javaPattern = new StringBuilder ( sqlPattern . length ( ) * 2 ) ; final int len = sqlPattern . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { char c = sqlPattern . charAt ( i ) ; if ( c == escapeChar ) { if ( i == ( len - 1 ) ) { // It should never reach here after the escape rule // checking. throw invalidEscapeSequence ( sqlPattern , i ) ; } char nextChar = sqlPattern . charAt ( i + 1 ) ; if ( SQL_SIMILAR_SPECIALS . indexOf ( nextChar ) >= 0 ) { // special character, use \ to replace the escape char. if ( JAVA_REGEX_SPECIALS . indexOf ( nextChar ) >= 0 ) { javaPattern . append ( ' ' ) ; } javaPattern . append ( nextChar ) ; } else if ( nextChar == escapeChar ) { javaPattern . append ( nextChar ) ; } else { // It should never reach here after the escape rule // checking. throw invalidEscapeSequence ( sqlPattern , i ) ; } i ++ ; // we already process the next char. } else { switch ( c ) { case ' ' : javaPattern . append ( ' ' ) ; break ; case ' ' : javaPattern . append ( "(?s:.*)" ) ; break ; case ' ' : javaPattern . append ( ' ' ) ; insideCharacterEnumeration = true ; i = sqlSimilarRewriteCharEnumeration ( sqlPattern , javaPattern , i , escapeChar ) ; break ; case ' ' : if ( ! insideCharacterEnumeration ) { throw invalidRegularExpression ( sqlPattern , i ) ; } insideCharacterEnumeration = false ; javaPattern . append ( ' ' ) ; break ; case ' ' : javaPattern . append ( "\\\\" ) ; break ; case ' ' : // $ is special character in java regex, but regular in // SQL regex. javaPattern . append ( "\\$" ) ; break ; default : javaPattern . append ( c ) ; } } } if ( insideCharacterEnumeration ) { throw invalidRegularExpression ( sqlPattern , len ) ; } return javaPattern . toString ( ) ; }
Translates SQL SIMILAR pattern to Java regex pattern .