Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

Groovy supports regular expressions natively using the ~"..." expression. Plus Groovy supports the =~ (create Matcher) and ==~ (matches regex) operators. e.g.

Error rendering macro 'code': Invalid value specified for parameter 'lang'
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"
  • No labels