Groovy Regexes


Groovy Regexes

I added native regular expression support in the form of an operator. Check it out and see if you like it.

In the current build of groovy you can now use regular expressions to compare strings with this simple syntax:

“string to compare” ~= “g.*t”

This will return a java.util.regex.Matcher that you can either check as a boolean which will .find() the regex in the string or you can manipulate it directly as a Matcher object. Additionally you can also use a more restrictive form:

“string” ~== “^s.*g$”

This returns a boolean only, true if the regex matches the entire string or false if it does not. You can find examples in the groovy unit test for regexes, RegularExpressionsTest.