| 1 Regular Expressions in Groovy |
| |
| Groovy supports regular expressions natively using the ~~~"..."~~ expression. Plus |
| Groovy supports the =~ (create Matcher) and ==~ (matches regex) operators. e.g. |
| |
| {code:groovysh} |
| import java.util.regex.Matcher |
| import java.util.regex.Pattern |
| |
| assert "cheesecheese" =~ "cheese" |
| |
| // lets create a regex Pattern |
| pattern = ~"foo" |
| assert pattern instanceof Pattern |
| assert pattern.matcher("foo").matches() |
| |
| // lets create a Matcher |
| matcher = "cheesecheese" =~ "cheese" |
| assert matcher instanceof Matcher |
| answer = matcher.replaceAll("edam") |
| |
| // lets do some replacement |
| cheese = ("cheesecheese" =~ "cheese").replaceFirst("nice") |
| assert cheese == "nicecheese" |
| {code} |
| |
| |
| |