Back references in Java regular expressions

Java Programming Java8Object Oriented Programming

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)

Example

Back references allow repeating a capturing group using a number like \# where # is the group number. See the example below:

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!");    
      }
   }
}


raja
Published on 23-Apr-2018 09:28:45
Advertisements