@@ -17,12 +17,7 @@ public LikeExpression(Interval expressionInterval, String expressionText, Expres
1717 super (expressionInterval , expressionText );
1818 this .internal = internal ;
1919 // Converting to regex is not the most performant impl, but it works
20- this .pattern = Pattern .compile ("^" +
21- pattern .replaceAll ("(?<!\\ \\ )\\ %" , ".*" )
22- .replaceAll ("(?<!\\ \\ )\\ _" , "." )
23- .replaceAll ("\\ \\ \\ %" , "%" )
24- .replaceAll ("\\ \\ _" , "_" ) + "$"
25- );
20+ this .pattern = convertLikePatternToRegex (pattern );
2621 }
2722
2823 @ Override
@@ -35,4 +30,38 @@ public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThr
3530
3631 return pattern .matcher (value ).matches ();
3732 }
33+
34+ private Pattern convertLikePatternToRegex (String pattern ) {
35+ StringBuilder builder = new StringBuilder ();
36+ builder .append ("^\\ Q" );
37+
38+ for (int i = 0 ; i < pattern .length (); i ++) {
39+ if (pattern .charAt (i ) == '\\' && i < pattern .length () - 1 ) {
40+ if (pattern .charAt (i + 1 ) == '%' ) {
41+ // \% case
42+ builder .append ('%' );
43+ i ++;
44+ continue ;
45+ } else if (pattern .charAt (i + 1 ) == '_' ) {
46+ // \_ case
47+ builder .append ('_' );
48+ i ++;
49+ continue ;
50+ }
51+ }
52+ if (pattern .charAt (i ) == '_' ) {
53+ // replace with .
54+ builder .append ("\\ E.\\ Q" );
55+ } else if (pattern .charAt (i ) == '%' ) {
56+ // replace with .*
57+ builder .append ("\\ E.*\\ Q" );
58+ } else {
59+ builder .append (pattern .charAt (i ));
60+ }
61+ }
62+
63+ builder .append ("\\ E$" );
64+
65+ return Pattern .compile (builder .toString ());
66+ }
3867}
0 commit comments