Java - Regular Expression Named Groups

Introduction

Java supports for named groups in regular expressions.

You can name a group then back reference groups using their names.

You can reference group names in replacement text and get the matched text using the group names.

Syntax

You need to use a pair of parentheses to create a group.

The start parenthesis is followed by a ? and a group name placed in angle brackets.

The format to define a named group is

(?<groupName>pattern)

The group name can only have letters and digits: a through z, A through Z, and 0 through 9.

The group name must start with a letter.

Example

The following is an example of a regular expression that uses three named groups.

The group names are areaCode, prefix, and lineNumber.

The regular expression is to match a 10-digit phone number.

\b(?<areaCode>\d{3})(?<prefix>\d{3})(?<lineNumber>\d{4})\b

You can use \k<groupName> to back reference the group named groupName.

You can rewrite the above regular expression that back reference the areaCode group as the following:

\b(?<areaCode>\d{3})\k<areaCode> (?<lineNumber>\d{4})\b

You can reference a named group in a replacement text as ${groupName}.

The following code uses three named groups to replacement text.

String regex = "\\b(?<areaCode>\\d{3})(?<prefix>\\d{3})(?<lineNumber>\\d{4})\\b";
String replacementText = "(${areaCode}) ${prefix}-${lineNumber}";

For named group, you can still refer to a group by its group number.

The above code can be rewritten as follows:

String regex = "\\b(?<areaCode>\\d{3})(?<prefix>\\d{3})(?<lineNumber>\\d{4})\\b";
String replacementText = "(${areaCode}) ${prefix}-$3";

The following code shows how to use group names in a regular expression and how to use the names in a replacement text.

Demo

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] args) {
    // Prepare the regular expression
    String regex = "\\b(?<areaCode>\\d{3})(?<prefix>\\d{3})(?<lineNumber>\\d{4})\\b";

    // Reference first two groups by names and the thrd oen as its number
    String replacementText = "(${areaCode}) ${prefix}-$3";

    String source = "1234567890, 1234567, and 1234567890";

    // Compile the regular expression
    Pattern p = Pattern.compile(regex);

    // Get Matcher object
    Matcher m = p.matcher(source);

    // Replace the phone numbers by formatted phone numbers
    String formattedSource = m.replaceAll(replacementText);

    System.out.println("Text: " + source);
    System.out.println("Formatted Text: " + formattedSource);
  }/*  w w  w  .ja  v a2 s .  c om*/
}

Result

Related Topic