regex

Groovy Regex Example

Photo of Akin KaldirogluAkin KaldirogluJuly 21st, 2015Last Updated: July 19th, 2015
0 397 5 minutes read

In this example I will show you how to use regular expressions in Groovy.

1. Groovy and Regular Expressions

Groovy is one of the most commonly used JVM languages and regular expressions (or simply regex) as a powerful technique to search and manipulate texts have been around us for more than a half century. As most of the languages Java has had the support for regex since version 1.4.

Groovy is a simple but powerful choice for Java developers when they need dynamic language capabilities with a smooth integration with Java. So it is quite possible for a Java developer to find herself writing Groovy code that makes use of regex.

Naturally Groovy is not part of JDK and must be separately downloaded from its web site at http://www.groovy-lang.org/. Alternatively you can just install Eclipse’s Groovy plugin in order to write and run Groovy scripts and programs in Eclipse. This plugin is also available at the Groovy’s site.

For this example I am going to use Java SE 8 and Groovy 2.4.3 in Eclipse Luna with its Groovy plugin installed.

Before delving into the details, let me give a brief information regarding how a regex look like. A regex is a character pattern to identify or manipulate a portion of a string. In a regex, each character stands for it own, so ‘a’ matches ‘a’. But if a character is used with one or more special characters namely metacharacters it may mean more than that. Most commonly used metacharacter is “[]”, a character class and it is used to construct different patterns. For example  “[a]” matches ‘a’ but if you use range metacharacter ‘-‘, “[a-z]” matches ‘a’ through ‘z’. Similarly “[^a]” matches anything except ‘a’.

2. Java Regex API

Groovy’s regex support is totally built upon Java’s regex API. But Groovy makes it easier to use by adding a set of concise notation. So in your Groovy code just import “java.util.regex.*” to be able to use Java’s regex API.

Two main constructs of Java’s regex API arePattern andMatcher classes. Pattern is the compiled form of regular expression itself. Its “compile(String regex)” method compiles the received regex pattern and returns a Pattern object.  Matcher is the unique implementation ofMatchResult interface and it performs match operations on a string by interpreting thePattern object. Methods onMatcher object can be used to find out if there is a match and much more information regarding the matches. So a common idiom to code regex in Java would be as follows:

Pattern pattern = Pattern.compile("... regex to be searched ...");Matcher matcher =  pattern.matcher("... text to search ...");boolean found = matcher.matches();

If you are interested in how many places the pattern matches and where the matches start and end you can use other methods on Matcher object. In this case you would replace the last line in previous idiom with something like this:

while (matcher.find()) {   System.out.format("Matching text: " + " \"%s\" starting at " + "index %d and ending                      at index %d.%n", matcher.group(), matcher.start(), matcher.end());}

Pattern class API gives lost of information regarding how to construct regex.

3. Using Regular Expressions in Groovy

As stated above Groovy totally uses Java’s API with some simple notational changes. As you might expect they are handy operators that replace API’s regex operations. They are “∼” pattern operator, “=∼” find operator and “==∼” match operator. Apparently “∼” pattern operator is a shortcut for creating a Pattern instance,  “=∼” find operator is a shortcut for creating a Matcher instance and finally “==∼” match operator is a shortcut for thematches method on Matcher class.  So Groovy’s idioms to find a match and to get a Matcher object become something like these:

   boolean b = myString ==~ pattern  // Note that ==~ requires an exact match!

and

 def matcher = myString =~ pattern

4. Source Code

Now let’s have two examples, the first one written in Java and then the second one written in Groovy. Both examples include a class that checks a pattern against a given string.

JavaRegexExample.java

 package com.javacodegeeks.groovy.regex;import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;public class JavaRegexExample {   private static String myString = "I love Groovy but to me Java is more lovely                                      than Groovy!";   public static void main(String[] args) {      Scanner scanner = new Scanner(System.in);      System.out.print("Enter a pattern: ");      Pattern pattern = Pattern.compile(scanner.next());      Matcher matcher = pattern.matcher(myString);      printMatcher(matcher);   }   * List all occurrences of the pattern.   * @param matcher Matcher object built upon the pattern and string.   */   private static void printMatcher(Matcher matcher) {      boolean found = false;      while (matcher.find()) {         found = true; System.out.format("Matching  text: " + " \"%s\" starting at                            " + "index %d and ending at index %d.%n",   matcher.group(), matcher.start(), matcher.end()); } if (!found)    System.out.println("No match found!");   }}

This is a Java class and you can run it as a Java application in Eclipse.

Here are some example outputs:

Enter a pattern: ooMatching  text:  "oo" starting at index 9 and ending at index 11.Matching  text:  "oo" starting at index 51 and ending at index 53.
Enter a pattern: I love Groovy but to me Java is more lovely than Groovy!Matching  text:  "I" starting at index 0 and ending at index 1.
Enter a pattern: o{2}Matching  text:  "oo" starting at index 9 and ending at index 11.Matching  text:  "oo" starting at index 51 and ending at index 53.

Now let’s have a look at the Groovy class that does almost the same thing.

GroovyRegexExample.groovy

 package com.javacodegeeks.groovy.regeximport java.util.regex.Matcherimport java.util.regex.Pattern;class GroovyRegexExample {   static String myString = "I love Groovy but to me Java is more lovely than Groovy!"   static main(args) {      def pattern      System.in.withReader {         print  'Enter a pattern: ' pattern = it.readLine()      }      exactMatch(pattern)      printMatcher(pattern)   }   /**   * Checks if there is an exact match.   * @param pattern Given pattern.   */   static void exactMatch(pattern){      def b = myString ==~ pattern      if(b)         println('Exact match!')      else         println('Not exact match!')   }   /**   * List all occurrences of the pattern.   * @param pattern Given pattern.   */   static void printMatcher(pattern) {      def matcher = myString =~ pattern      while (matcher.find()) {         System.out.format("Matching  text: " + " \"%s\" starting at " +                            "index %d and ending at index %d.%n",    matcher.group(), matcher.start(), matcher.end())      }   }}

This is a Groovy class and you can run it as a Groovy script in Eclipse.

Here are some example outputs:

Enter a pattern: ooNot exact match!Matching text: "oo" starting at index 9 and ending at index 11.Matching text: "oo" starting at index 51 and ending at index 53.
Enter a pattern: I love Groovy but to me Java is more lovely than Groovy!Exact match!Matching text: "I love Groovy but to me Java is more lovely than Groovy!" starting at index 0 and ending at index 56.
Enter a pattern: o{2}Not exact match!Matching text: "oo" starting at index 9 and ending at index 11.Matching text: "oo" starting at index 51 and ending at index 53.

5. Closing Thoughts

Regular expressions are very powerful tool when playing with strings. Their usage in Groovy is easy. Important thing is to build correct and effective patterns using a variety of metacharacters.

6. Download The Eclipse Project

This is an example of regex in Groovy.

Download
You can download the Eclipse project of the example here :GroovyRegexExample
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Akin KaldirogluAkin KaldirogluJuly 21st, 2015Last Updated: July 19th, 2015
0 397 5 minutes read
Photo of Akin Kaldiroglu

Akin Kaldiroglu

Akin Kaldiroglu is a Java consultant and trainer living in İstanbul, Turkey. He has been involved in many enterprise projects with different roles since mid 90s. At the beginning he started coding with C and C++ and then switched to Java when it arrived. He loves developing software and writing code in Java. He is a fan of clean and object-oriented code.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.