
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to match n number of occurrences of an expression using Java RegEx?
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
- Related Articles
- How to match digits using Java Regular Expression (RegEx)
- Match all occurrences of a regex in Java
- How to match non-digits using Java Regular Expression (RegEx)
- How to match a range of characters using Java regex
- How to match end of the input using Java RegEx?
- How to match any character using Java RegEx
- How to match word characters using Java RegEx?
- How to match word boundaries using Java RegEx?
- How to match a fixed set of characters using Java RegEx
- How to match the beginning of the input using Java RegEx?
- How match a string irrespective of case using Java regex.
- How to match end of a particular string/line using Java RegEx
- How to match beginning of a particular string/line using Java RegEx
- How to match one of the two given expressions using Java RegEx?
- How to match non-word boundaries using Java RegEx?

Advertisements