Regular Expressions in Java allow matching, searching, and manipulating text. Quantifiers control how many times a character, character class, or group can repeat in a pattern. They make regex flexible and efficient, helping in validation like numbers, codes, and identifiers.
Common Quantifier Symbols Used in Java
These symbols specify how many times a pattern can repeat. They are forms used to define quantifier behavior, not types.
- X*: Zero or more occurrences of X
- X?: Zero or one occurrence of X
- X+: One or More occurrences of X
- X{n}: At least n occurrences of X
- X{n, }: At least n occurrences of X
- X{n, m}: Count of occurrences of X is from n to m
public class GFG {
public static void main(String[] args) {
String input = "0007";
System.out.println(input.matches("0*\\d"));
}
}
Output
true
Explanation:
- 0* allows zero or more occurrences of 0
- \d matches a digit
- The string "0007" satisfies the pattern
Types of Quantifiers in Java Regex
1. Greedy Quantifier
Greedy quantifiers try to match as much of the input as possible while still allowing the overall pattern to match. This is the default behavior in Java Regex.
public class GFG {
public static void main(String[] args) {
String regex = "a+";
System.out.println("aaaa".matches(regex));
}
}
Output
true
Explanation:
- + is greedy by default
- It matches the maximum possible number of a characters
2. Reluctant Quantifier
Reluctant quantifiers try to match as little of the input as possible. They are created by appending ? to a quantifier symbol.
public class GFG {
public static void main(String[] args) {
String regex = "a+?";
System.out.println("aaaa".matches(regex));
}
}
Output
true
Explanation:
- +? makes the quantifier reluctant
- It matches the minimum number of characters required
3. Possessive Quantifier
Possessive quantifiers match as much as possible and do not backtrack. They are created by appending + to a quantifier symbol.
public class GFG {
public static void main(String[] args) {
String regex = "a++a";
System.out.println("aaaa".matches(regex));
}
}
Output
false
Explanation: In this example, a++ consumes all a characters and does not allow backtracking. As a result, the remaining a in the pattern cannot be matched, and the match fails.
Greedy vs Reluctant vs Possessive Quantifiers
Aspect | Greedy Quantifiers | Reluctant (Lazy) Quantifiers | Possessive Quantifiers |
|---|---|---|---|
Matching Behaviour | Matches as much as possible while still allowing the overall match | Matches as little as possible to satisfy the pattern | Matches as much as possible without giving up characters |
Backtracking | Allows backtracking | Allows backtracking | Does not allow backtracking |
Syntax / Usage | Default quantifiers (*, +, {m,n}) | Append ? after the quantifier (*?, +?, {m,n}?) | Append + after the quantifier (*+, ++, {m,n}+) |
Control Over Matching | Least control (default behavior) | Highest control over minimal matching | Strictest control, no backtracking |
Performance | Can be slower due to backtracking | Can be slower due to frequent backtracking | Often faster, but may cause match failure |
Common Use Case | General-purpose matching | Precise or minimal matching scenarios | Performance-critical patterns where backtracking is unnecessary |