Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java 8 regex match group – named capturing groups

farenda 2016-12-06 0

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:

  • Learn more cool features at Java Util Tutorials!
Share with the World!
Categories Java Tags java, java-util, java8, regex
Previous: Composed Method Pattern explained
Next: Graph representation

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok