@ SuppressWarnings ( "unchecked" ) public static Expression < String > likeToRegex ( Expression < String > expr , boolean matchStartAndEnd ) { // TODO : this should take the escape character into account if ( expr instanceof Constant < ? > ) { final String like = expr . toString ( ) ; final StringBuilder rv = new StringBuilder ( like . length ( ) + 4 ) ; if ( matchStartAndEnd && ! like . startsWith ( "%" ) ) { rv . append ( ' ' ) ; } for ( int i = 0 ; i < like . length ( ) ; i ++ ) { char ch = like . charAt ( i ) ; if ( ch == ' ' || ch == ' ' || ch == ' ' ) { rv . append ( ' ' ) ; } else if ( ch == ' ' ) { rv . append ( ".*" ) ; continue ; } else if ( ch == ' ' ) { rv . append ( ' ' ) ; continue ; } rv . append ( ch ) ; } if ( matchStartAndEnd && ! like . endsWith ( "%" ) ) { rv . append ( ' ' ) ; } if ( ! like . equals ( rv . toString ( ) ) ) { return ConstantImpl . create ( rv . toString ( ) ) ; } } else if ( expr instanceof Operation < ? > ) { Operation < ? > o = ( Operation < ? > ) expr ; if ( o . getOperator ( ) == Ops . CONCAT ) { Expression < String > lhs = likeToRegex ( ( Expression < String > ) o . getArg ( 0 ) , false ) ; Expression < String > rhs = likeToRegex ( ( Expression < String > ) o . getArg ( 1 ) , false ) ; if ( lhs != o . getArg ( 0 ) || rhs != o . getArg ( 1 ) ) { return operation ( String . class , Ops . CONCAT , lhs , rhs ) ; } } } return expr ; }
Convert the given like pattern to a regex pattern