2323import com .google .protobuf .Duration ;
2424import com .google .protobuf .MessageLiteOrBuilder ;
2525import com .google .re2j .Pattern ;
26+ import com .google .re2j .PatternSyntaxException ;
2627import dev .cel .common .CelOptions ;
2728import dev .cel .common .annotations .Internal ;
2829import dev .cel .common .exceptions .CelDivideByZeroException ;
2930import dev .cel .common .exceptions .CelIndexOutOfBoundsException ;
31+ import dev .cel .common .exceptions .CelInvalidArgumentException ;
3032import dev .cel .common .exceptions .CelNumericOverflowException ;
3133import dev .cel .common .internal .Converter ;
3234import dev .cel .common .values .NullValue ;
@@ -76,16 +78,28 @@ public static java.time.Duration createJavaDurationFromString(String d) {
7678 }
7779 }
7880
79- public static boolean matches (String string , String regexp , CelOptions celOptions ) {
80- Pattern pattern = Pattern .compile (regexp );
81+ /**
82+ * Compiles the given RE2J regex pattern string and validates it against {@link
83+ * CelOptions#maxRegexProgramSize()}.
84+ */
85+ public static Pattern compileRegexPattern (String regexp , CelOptions celOptions ) {
86+ Pattern pattern ;
87+ try {
88+ pattern = Pattern .compile (regexp );
89+ } catch (PatternSyntaxException e ) {
90+ throw new CelInvalidArgumentException (e );
91+ }
8192 int maxProgramSize = celOptions .maxRegexProgramSize ();
8293 if (maxProgramSize >= 0 && pattern .programSize () > maxProgramSize ) {
83- throw new IllegalArgumentException (
94+ throw new CelInvalidArgumentException (
8495 String .format (
8596 "Regex pattern exceeds allowed program size. Allowed: %d, Provided: %d" ,
8697 maxProgramSize , pattern .programSize ()));
8798 }
99+ return pattern ;
100+ }
88101
102+ public static boolean matches (String string , Pattern pattern , CelOptions celOptions ) {
89103 if (!celOptions .enableRegexPartialMatch ()) {
90104 // Uses re2 for consistency across languages.
91105 return pattern .matcher (string ).matches ();
@@ -95,6 +109,10 @@ public static boolean matches(String string, String regexp, CelOptions celOption
95109 return pattern .matcher (string ).find ();
96110 }
97111
112+ public static boolean matches (String string , String regexp , CelOptions celOptions ) {
113+ return matches (string , compileRegexPattern (regexp , celOptions ), celOptions );
114+ }
115+
98116 /** Concatenates two lists into a new list. */
99117 public static <E > List <E > concat (List <E > first , List <E > second ) {
100118 if (first instanceof ConcatenatedListView ) {
0 commit comments