Capturing groups and back references in Java Regex

Java 8Object Oriented ProgrammingProgramming

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".

Capturing groups are numbered by counting their opening parentheses from the left to the right.

In the expression ((A)(B(C))), for example, there are four such groups -

  • ((A)(B(C)))
  • (A)
  • (B(C))
  • (C)

Back references allow repeating a capturing group using a number like \# where # is the group

number. See the example below −

Example

Live Demo

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Tester {
   public static void main(String[] args) {
      //2 followed by 2 five times
      String test = "222222";
      String pattern = "(\\d\\d\\d\\d\\d)";
      Pattern r = Pattern.compile(pattern);
      Matcher m = r.matcher(test);
      if (m.find( )) {
         System.out.println("Matched!");
      }else {
         System.out.println("not matched!");
      }
      //\1 as back reference to capturing group (\d)
      pattern = "(\\d)\\1{5}";
      r = Pattern.compile(pattern);
      m = r.matcher(test);
      if (m.find( )) {
         System.out.println("Matched!");
      }else {
         System.out.println("not matched!");
      }
   }
}

Output

Matched!
Matched!
raja
Published on 07-Feb-2018 17:06:22
Advertisements