English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Subexpression “(?:re)” in Java regular expressions

Subexpression/Meta character " (?:re) "Group the regular expression without remembering the matched text."

Example

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternExample {
   public static void main(String args[]) {
      //Read a string from the user
      System.out.println("Enter a String");
      Scanner sc = new Scanner(System.in);
      String input = sc.next();
      String regex = "(?:[0-9]);
      //Compile the regular expression
      Pattern pattern = Pattern.compile(regex);
      //Retrieve the matcher object
      Matcher matcher = pattern.matcher(input);
      //Verify whether a match has occurred
      boolean bool = matcher.find();
      if(bool) {
         System.out.print("Match found");
      } else {
         System.out.print("Match not found");
      }
   }
}

Output Result

Enter a String
9848022338
Match found