Java - Groups and Back Referencing


You can treat characters as a unit by using them as a group.

A regular expression group is marked by parentheses.

For example, (ab), ab(z), ab(ab)(xyz), (the((is)(is))).

Each group in a regular expression has a group number which starts at 1.

Matcher class groupCount() method returns the number of groups in the pattern.

group 0 refers to the entire regular expression, which is not reported by the groupCount() method.

Each left parenthesis inside a regular expression marks the start of a new group.


For Regular Expression AB(XY)

Group Number Group Text
0 AB(XY)
1 (XY)

For Regular Expression: (AB)(XY)

Group Number Group Text
0 (AB)(XY)
1 (AB)
2 (XY)

For Regular Expression: ((A)((X)(Y)))

Group Number Group Text
0 ((A)((X)(Y)))
1 ((A)((X)(Y)))
2 (A)
3 ((X)(Y))
4 (X)
5 (Y)

For Regular Expression: ABCD

Group Number Group Text

The following code shows how to use Groups in Regular Expressions.


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

public class Main {
  public static void main(String[] args) {
    // Prepare a regular expression: A group of 3 digits followed by 7 digits.
    String regex = "\\b(\\d{3})\\d{7}\\b";

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

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

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

    // Start matching and display the found area codes
    while (m.find()) {
      String phone =;//from w w w.  j a  va 2s . c  o  m
      String areaCode =;
      System.out.println("Phone: " + phone + ", Area Code: " + areaCode);


Related Topics