Groovy supports regular expressions natively using the ~"pattern" expression, which compiles a Java Pattern object from the given pattern string. Groovy also supports the =~ (create Matcher) and ==~ (matches regex) operators.
For matchers having groups, matcher[index] is either a matched String or a List of matched group Strings, since jsr-03 release.
The pattern can be expressed more simply using the "/" delimiter for the pattern, so we don't have to double all the backslashes.
Since a Matcher coerces to a boolean by calling its find method, the =~ operator is consistent with the simple use of Perl's =~ operator, when it appears as a predicate (in 'if', 'while', etc.). The "stricter-looking" ==~ operator requires an exact match of the whole subject string.
Regular expression support is imported from Java. Java's regular expression language and API is documented here.
Goal: Capitalize words at the beginning of each line:
Goal: Capitalize every word in a string:
Add .toLowerCase() to make the rest of the words lowercase