Groovy Regex Example
1. Introduction
Regular Expression is a character sequence defines search pattern especially for pattern matching with strings. You may see Regular Expression as Regex or Regexp in software world. In this tutorial, I will show you how to use regex operations in Groovy by using pretty easy methods.
2. Quick Start
In Groovy, you can define a pattern by using the tilda operator (~) for a String. When you define a pattern by using tilda operator, you will get a result in a type java.util.regex.Pattern
. And this means, you can apply all the rules that you do in Java code. You can see quick example below.
GroovyRegex.groovy
package com.javacodegeeks.groovy.regex class GroovyRegex { static main(args) { def ipAddress = ~/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ println ipAddress.class.name // java.util.regex.Pattern } }
You can see that the pattern class name is java.util.regex.Pattern
. In order to test whether if provided string matches with the pattern, you can use following.
GroovyRegexMatch.groovy
package com.javacodegeeks.groovy.regex class GroovyRegexMatch { static main(args) { def nameRegex = ~'john' println nameRegex.matcher("john").matches() // true def ipAddressRegex = ~/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ println ipAddressRegex.matcher("127.0.0.1").matches() // true } }
On line 06
, we defined a pattern and we tested string “john” whether it matches with pattern ~'john'
or not on line 07
. On line 09
, we have defined a pattern for ip address and checked the string 127.0.0.1
for pattern matching in same way on line 10
.
3. Advanced Usage
Groovy lets you to create pattern matchers easily. For example, if you need to test a string if it starts with ‘L’ and ends with ‘s’, you can use following.
GroovyRegexMatchAdvanced.groovy
package com.javacodegeeks.groovy.regex class GroovyRegexMatchAdvanced { static main(args) { def pattern = ~'L....e' println pattern.matcher("Little").matches() // true } }
On line 06
we have defined the pattern. It says that, it will start with 'L'
and then has 4 characters, and finally it ends with 'e'
. On line 07
, it matches with the string 'Little'
which starts with 'L'
, ends with 'e'
and has 'ittl'
in the middle.
GroovyRegexMatchAdvanced.groovy
package com.javacodegeeks.groovy.regex class GroovyRegexMatchAdvanced { static main(args) { def pattern = ~'L....e' println pattern.matcher("Little").matches() // true } }
You can also use isCase() for the pattern matching in Groovy. Let say that, you only know the starting and ending letter of the string and you want to check whetter it matches with the pattern or not. You can use following for that case.
GroovyRegexMatchAdvanced.groovy
package com.javacodegeeks.groovy.regex class GroovyRegexMatchAdvanced { static main(args) { def isCasePattern = ~/L\w+e/ println isCasePattern.isCase("Little") } }
On line 06
, the pattern contains starting letter L
and ending letter e
, it contains any length of whitespace character inside L
and e
.
GroovyRegexMatchAdvanced.groovy
package com.javacodegeeks.groovy.regex class GroovyRegexMatchAdvanced { static main(args) { def grepPattern = ~/A\w+/ def cities = ['Alabama', 'Los Angeles', 'Arizona'] println cities.grep(grepPattern) // [Alabama, Arizona] } }
In above example, we have defined our pattern as ~/A\w+/
which means, things that start with A
and then we grep the cities that matches with pattern on line 08
.
4. Matchers
In previous examples, we have used an expression like below.
pattern.matcher("Little")
This is what a matchers is. We have called matches()
function over matchers. In Groovy, there is a easier way to define matchers and run matches()
function. You can see below for example matcher.
GroovyRegexMatcher.groovy
package com.javacodegeeks.groovy.regex class GroovyRegexMatcher { static main(args) { def matcher = ("127.0.0.1" =~ /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/) println matcher.class.name // java.util.regex.Matcher } }
As you can see, we have used =~
for defining a matcher. As a result, we got an object that instance of java.util.regex.Matcher
. You can call this an IP address matcher. Let’s see a couple of examples to understand matcher deeper.
GroovyRegexMatcher.groovy
package com.javacodegeeks.groovy.regex class GroovyRegexMatcher { static main(args) { def numbers = ('5 is greater than 4' =~ /\d+/) println numbers // java.util.regex.Matcher[pattern=\d+ region=0,19 lastmatch=] println numbers.size() // 2 println numbers[0] // 5 } }
In above example, on line 06
we have defined a matcher '5 is greater than 4' =~ /\d+/
which says that something matches with numbers. This will find two numbers in string which are 5
, and 4
. On line 07, it prints the value of the matcher object. If you want to get the values found by matcher, you can use array notation as on line 09
.
5. Conclusion
Groovy lets us to use regex as in the java but it has some extra features and easy usages. There are two section in regex world which are Pattern and Matcher. Pattern is the regular expression we define. Matcher is an expression with provided string and the regular expression. We call matches()
function over matchers to find the things you want
You can download the full source code of this example as an Eclipse project here: GroovyRegexExample