Quantifiers in Java regular expressions (Regex) specify how many times a character, character class, or group can occur in a pattern.
- Support exact, minimum, maximum, and unlimited repetitions.
- Control matching behavior through greedy, reluctant, and possessive quantifiers.
public class Geeks{
public static void main(String[] args){
String input = "0007";
System.out.println(input.matches("0*\\d"));
}
}
Output
true
Explanation: 0* matches zero or more zeros, while \d matches a single digit from 0 to 9 by default. Therefore, "0007" matches the entire pattern.
Common Quantifiers
Quantifier | Description | Example |
|---|---|---|
X* | Zero or more occurrences | a* |
X+ | One or more occurrences | a+ |
X? | Zero or one occurrence | a? |
X{n} | Exactly n occurrences | a{3} |
X{n,} | At least n occurrences | a{2,} |
X{n,m} | Between n and m occurrences, inclusive | a{2,4} |
Here, X represents a character, character class, or group.
Types of Quantifiers
Quantifiers are classified into three types based on their matching and backtracking behavior.
- Greedy Quantifiers: Greedy quantifiers initially match as much input as possible and backtrack when necessary to satisfy the remaining pattern. This is the default quantifier behavior in Java.
- Reluctant Quantifiers: Reluctant quantifiers, also called lazy quantifiers, initially match as little input as possible and expand the match when required. They are formed by appending ? to a basic quantifier.
- Possessive Quantifiers: Possessive quantifiers match as much input as possible without giving up consumed characters through backtracking. They are formed by appending + to a basic quantifier.
Example: Comparing greedy, reluctant, and possessive quantifiers.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Geeks{
public static void main(String[] args){
String input = "<b>Java</b><b>Regex</b>";
// Greedy matching
Matcher greedy = Pattern.compile("<b>.*</b>").matcher(input);
while (greedy.find()){
System.out.println("Greedy: " + greedy.group());
}
// Reluctant matching
Matcher reluctant = Pattern.compile("<b>.*?</b>").matcher(input);
while (reluctant.find()){
System.out.println("Reluctant: " + reluctant.group());
}
// Greedy and possessive matching
System.out.println("Greedy: " + "aaaa".matches("a+a"));
System.out.println("Possessive: " + "aaaa".matches("a++a"));
}
}
Output
Greedy: <b>Java</b><b>Regex</b> Reluctant: <b>Java</b> Reluctant: <b>Regex</b> Greedy: true Possessive: false
Explanation:
- .* consumes as much text as possible before backtracking to match the last closing tag.
- .*? consumes only enough text to match the nearest closing tag, producing two separate matches.
- a+a succeeds because a+ backtracks and leaves one character for the final a.
- a++a fails because a++ consumes all four characters without releasing any for the final a.
Greedy Vs Reluctant vs Possessive Quantifiers
Occurrences | Greedy | Reluctant | Possessive |
|---|---|---|---|
Zero or more | X* | X*? | X*+ |
One or more | X+ | X+? | X++ |
Zero or one | X? | X?? | X?+ |
Exactly n | X{n} | X{n}? | X{n}+ |
At least n | X{n,} | X{n,}? | X{n,}+ |
Between n and m | X{n,m} | X{n,m}? | X{n,m}+ |
Initial repetition | Maximum possible | Minimum required | Maximum possible |
Quantifier backtracking | Allowed | Allowed | Not allowed |
Note: Possessive quantifiers can improve performance by avoiding unnecessary backtracking. However, they may cause a match to fail where greedy or reluctant quantifiers would succeed.