Java Programming Tutorials

Java programming tutorials with many code examples!

Java 8 regex match group – named capturing groups

Java 8 regular expressions have been improved with names for capturing groups. In this post we show how to used them to improve Java code maintainability!

Before Java 8 regex capturing group by with index

In the following short code we’re going to use regular expressions to extract flight information from sample data. To do that we use standard Pattern and Matcher classes from Java, and extract capturing groups using their corresponding indices:

String data = "Flight AA JFK.101.KRK[2016-12-06]";
Pattern flightPattern = Pattern.compile("\\w+" + " "
        + "(..) "
        + "(...)\\." + "(\\d+)\\." + "(...)"
        + "\\[(\\d+-\\d+-\\d+)\\]");
Matcher flight = flightPattern.matcher(data);
flight.find();
System.out.println("Airline: " + flight.group(1));
System.out.println("Origin: " + flight.group(2));
System.out.println("Number: " + flight.group(3));
System.out.println("Destination: " + flight.group(4));
System.out.println("Departure date: " + flight.group(5));

The code is not very readable, and it’s very easy to make a mistake while taking regex capturing groups using indices. One good thing is that the above code does what it supposed to:

Airline: AA
Origin: JFK
Number: 101
Destination: KRK
Departure date: 2016-12-06

Java 8 regex – named capturing groups

Although the above regex pattern works, since Java 8 we can use name capturing groups using ?<name> in front of the regex capturing group. Then we can refer to such group using given name:

String data = "Flight AA JFK.101.KRK[2016-12-06]";
Pattern flightPattern = Pattern.compile("\\w+" + " "
    + "(?<airline>..) "
    + "(?<origin>...)\\." + "(?<number>\\d+)\\." + "(?<destination>...)"
    + "\\[(?<deptDate>\\d+-\\d+-\\d+)\\]");
Matcher flight = flightPattern.matcher(data);
flight.find();
System.out.println("Airline: " + flight.group("airline"));
System.out.println("Origin: " + flight.group("origin"));
System.out.println("Number: " + flight.group("number"));
System.out.println("Destination: " + flight.group("destination"));
System.out.println("Departure date: " + flight.group("deptDate"));

I like that! :-) The regex pattern gives the same result, but the code is much more readable and when someone tinkers around group(name) the errors are easier to spot – we clearly see what we are trying to get.

References:

Share with the World!