Non capturing groups Java regular expressions:


Using capturing groups you can treat multiple characters as a single unit. You just need to place the characters to be grouped inside a set of parentheses. For example −

(.*)(\d+)(.*)

If you are trying to match multiple groups the match results of each group is captured. You can get the results a group by passing its respective group number to the group() method. 1,2, 3 etc.. (from right to left) group 0 indicates the whole match.

Example

 Live Demo

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CapturingGroups {
   public static void main( String args[] ) {
      System.out.println("Enter input text");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      String regex = "(.*)(\d+)(.*)";
      //Create a Pattern object
      Pattern pattern = Pattern.compile(regex);
      //Now create matcher object.
      Matcher matcher = pattern.matcher(input);
      if (matcher.find( )) {
         System.out.println("Found value: " + matcher.group(0) );
         System.out.println("Found value: " + matcher.group(1) );
         System.out.println("Found value: " + matcher.group(2) );
         System.out.println("Found value: " + matcher.group(3) );
      } else {
            System.out.println("NO MATCH");
      }
   }
}

Output

Enter input text
sample data with 1234 (digits) in middle
Found value: sample data with 1234 (digits) in middle
Found value: sample data with 123
Found value: 4
Found value: (digits) in middle

Non capturing groups

The non-capturing group provides the same functionality of a capturing group but it does not captures the result

For example, if you need to match a URL or a phone number from a text using groups, since the starting part of the desired sub strings is same you need not capture the results of certain groups in such cases you can use non capturing groups. A non-capturing group starts with (?: and ends with ).

Example

 Live Demo

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CapturingGroups {
   public static void main( String args[] ) {
      System.out.println("Enter input text");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      String regex = "(.*)(?:\d+)(.*)";
      //Create a Pattern object
      Pattern pattern = Pattern.compile(regex);
      //Now create matcher object.
      Matcher matcher = pattern.matcher(input);
      if (matcher.find( )) {
         System.out.println("Found value: " + matcher.group(0) );
         System.out.println("Found value: " + matcher.group(1) );
         System.out.println("Found value: " + matcher.group(2) );
      } else {
         System.out.println("NO MATCH");
      }
   }
}

Output

Enter input text
sample data with 1234 (digits) in middle
Found value: sample data with 1234 (digits) in middle
Found value: sample data with 123
Found value: (digits) in middle

Note: The non-capturing group will not be included in the group count.

Updated on: 21-Feb-2020

621 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements