How to match n number of occurrences of an expression using Java RegEx?

JavaObject Oriented ProgrammingProgramming

The greedy quantifiers provided by Java allows you to match the multiple occurrences of an expression. Where,

  • Exp{n} impels the occurrence of the expression exp exactly n times.

  • Exp{n,} impels the occurrence of the expression exp at least n times.

  • Exp{n, m} impels occurrence of the expression exp at least n and utmost m times.

Example 1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main( String args[] ) {
      //regular expression to accept 5 letter word
      String regex = "\\w{5}";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter 5 input strings: ");
      String input[] = new String[5];
      for (int i=0; i<5; i++) {
         input[i] = sc.nextLine();
      }
      //Creating a Pattern object
      Pattern p = Pattern.compile(regex);
      for(int i=0; i<5;i++) {
         //Creating a Matcher object
         Matcher m = p.matcher(input[i]);
         if(m.find()) {
            System.out.println(input[i]+": accepted");
         } else {
            System.out.println(input[i]+": not accepted");
         }
      }
   }
}

Output

Enter 5 input strings:
rama
raja
raghu
megha
malya
rama: not accepted
raja: not accepted
raghu: accepted
megha: accepted
malya: accepted

Example 2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main( String args[] ) {
      //Regular expression to match a string of non-word with length 2 to 6
      String regex = "\\W{2,6}";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter 5 input strings: ");
      String input[] = new String[5];
      for (int i=0; i<5; i++) {
         input[i] = sc.nextLine();
      }
      //Creating a Pattern object
      Pattern p = Pattern.compile(regex);
      for(int i=0; i<5;i++) {
         //Creating a Matcher object
         Matcher m = p.matcher(input[i]);
         if(m.find()) {
            System.out.println(input[i]+" matched");
         }
      }
   }
}

Output 1

Enter 5 input strings:
hello how are you
#$#%
#
#$@%%#&#&
sample text
#$#% matched
#$@%%#&#& matched

Example 3

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main(String args[] ) {
      String regex = "[a-zA-Z]{1,20}";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter students name:");
      String name = sc.nextLine();
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(name);
      if(m.matches()) {
         System.out.println("Name is appropriate");
      } else {
         System.out.println("Name is inappropriate");
      }
   }
}

Output

Enter students name:
Mouktika
Name is appropriate
raja
Published on 19-Nov-2019 07:33:44
Advertisements