Last published: 12 March 2004 |
Using Groovy
User Guide
Articles
|
Groovy supports regular expressions natively using the ~"..." expression. Plus Groovy supports the =~ (create Matcher) and ==~ (matches regex) operators. e.g. import java.util.regex.Matcher import java.util.regex.Patternassert "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" |
© 2003-2004, The Codehaus |