GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Java Quantifiers - GeeksforGeeks Java QuantifiersLast Updated : 7 Oct, 2026 Java Quantifiers are special symbols in Regular Expressions (Regex) that specify how many times a character, character class, or group can occur in a pattern. They make regex more powerful and flexible for tasks such as input validation, searching, parsing, and text manipulation.
Output true Explanation: 0* matches zero or more occurrences of 0, while \d represents a digit. Therefore, "0007" matches the pattern. Common Quantifier Symbols Used in JavaThese symbols specify how many times a character, character class, or group can occur in a regular expression. They represent the basic quantifier symbols, while their matching behavior can be Greedy, Reluctant, or Possessive.
Here, X can represent a character, character class, or group. For example:
matches:
Types of Quantifiers in Java Regex1. Greedy QuantifierGreedy quantifiers try to match as much input as possible while still allowing the complete regular expression to match. This is the default behavior of quantifiers in Java. Common greedy quantifiers include:
Output <b>Java</b><b>Regex</b> Explanation The .* part is greedy. It consumes as many characters as possible before allowing the final </b> in the pattern to match. Therefore, the regex matches from the first <b> to the last </b> 2. Reluctant QuantifiersReluctant quantifiers, also called lazy quantifiers, try to match as little input as possible while still allowing the complete regex to match. They are created by adding ? after a quantifier. Examples include:
Output <b>Java</b> <b>Regex</b> Explanation: The .*? part is reluctant. Instead of consuming as much text as possible, it consumes only the minimum number of characters needed to reach the next </b>. So the matches are:
instead of:
3. Possessive QuantifierPossessive quantifiers try to match as much input as possible and do not backtrack. They are created by adding + after a quantifier. Examples include:
Output false Explanation: a+a returns true because the greedy a+ can backtrack and leave one a for the final a in the pattern. a++a returns false because the possessive a++ consumes all characters and does not allow backtracking. Greedy vs Reluctant vs Possessive Quantifiers
Advantages of Java Quantifiers
Comment Explore | ||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |