Sunday, October 16, 2011

Named Groups In Regular Expressions - Java 7

One of the new features added in Java 7 was the introduction of named groups in regular expressions through the java.util.regex package. This article covers the various features of named groups in regular expressions and their syntax in Java.

Parts of a Regular Expression can be grouped together by placing them inside round brackets. A major advantage of grouping is that various regex operators can be applied to these groups. Grouping is also useful for back-referencing a match. This allows developers to write regular expressions involving complex repetition patterns more easily.

The value of the captured group can be retrieved directly or be referenced in replacement patterns for changing the format of an input string. Though Java supported Regular Expressions and Grouping through the java.util.regex package from Java 1.4, it was limited to numbered groups.

Here's an example showing how numbered groups can be used in Java. To read more about Grouping and Backreferences, check this out.

Parallel to numbered groups exists named groups. Named groups do not change the underlying concept of grouping and back-referencing but provide more readability to the regular expressions and make back-referencing easier for developers since remembering names is easier than remembering the relative position of a group.

Named groups are already present in languages like Perl, Python and .NET but wasn't available to Java prior to Java 7. Well, better late than never.

The newly added RegEx constructs in Java 7 are as follows -

  • (?) defines a named group "name"
  • \\k back-references a named group "name"
  • ${name} can be used to reference a captured group in a replacement string
  • group(String name) returns the value of the captured group

Here's an example showing how named groups can be used in Java 7. To read more about Named Groups, check this out.

However if you are using Java 7, I would strongly recommend using the java.util.regex package but if you are using a Java version older than 7, there are other alternatives available for named groups in Java like Named-RegExp and JRegex. These alternatives were highly recommended by other developers at stack overflow.

1 comment:

Vee Eee Technologies said...

Thanks for the post. It was very interesting and meaningful.