Regular Expressions
The JDK package java.util.regex contains the API for pattern-matching with regular expressions (RegEx). For a detailed discussion of RegEx, refer to Jeffrey Friedl’s Mastering Regular Expressions [Fri97]. String’s replaceFirst and replaceAll methods, among other methods, make good use of RegEx pattern-matching. Groovy adds operators and symbols to make it easier to program with RegEx.
Groovy provides the operator ~ to easily create a RegEx pattern. This operator maps to String’s negate method:
WorkingWithStrings/RegEx.groovy | |
| obj = ~"hello" |
| println obj.getClass().name |
The output shows the type of the instance created:
| java.util.regex.Pattern |
The previous example shows that ~ applied to String creates an instance of ...
Get Programming Groovy 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.