
- 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
Types of quantifiers in Java regex
If you want to specify the number of occurrences while constructing a regular expression you can use quantifiers. Java supports three types of quantifiers namely: greedy quantifiers, reluctant quantifiers and possessive quantifiers.
Greedy quantifiers − Greedy quantifiers are the default quantifiers. A greedy quantifier matches as much as possible from the input string (longest match possible) if match not occurred it leaves the last character and matches again.
Example
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "[0-9]+"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); System.out.println(""Matched text: ); while (matcher.find()) { System.out.println(matcher.group()); } } }
Output
Enter input text: Matched text: 45545
Reluctant quantifiers − a non-greedy/reluctant quantifier matches as little as possible, initially the non-greedy quantifier matches the first character if match not occurred it adds another character from the input string and tries to match. If you place a “?” after a greedy quantifier it becomes reluctant or non-greedy quantifier.
Example
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "[0-9]+?"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); while (matcher.find()) { System.out.println(matcher.group()); } } }
Output
Enter input text: 12345678 1 2 3 4 5 6 7 8
Possessive quantifiers − A possessive quantifier is similar to a greedy quantifier the only difference is it tries to match as many character as it can initially and, if match not occurred unlike greedy quantifier it does not backtrack.
If you place a "+" after a greedy quantifier it becomes possessive quantifier. Following is the list of possessive quantifiers:
Example
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "[0-9]++"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); while (matcher.find()) { System.out.print(matcher.group()); System.out.println(); } } }
Output
Enter input text: 45678 45678
- Related Articles
- Regex quantifiers in Java Regular Expressions
- Quantifiers in Java
- Greedy quantifiers Java Regular expressions in java.
- Explain quantifiers in Java regular expressions
- Possessive quantifiers Java Regular expressions
- Reluctant quantifiers Java Regular expressions
- Regex metacharacters in Java Regex
- Quantifiers in C#
- Regex named groups in Java
- Methods of the Matcher class in Java Regex
- Methods of the Pattern class in Java Regex
- Match all occurrences of a regex in Java
- Explain C# Quantifiers in regular expression
- Character class p{javaWhitespace} Java regex in java.
- Named Capturing groups in Java Regex
