Open In App

Java Quantifiers

Last Updated : 10 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Quantifiers in Java allow users to specify the number of occurrences to match against. These are used with regular expressions to specify the number of times a particular pattern or character can appear in the Input.

Below are some commonly used quantifiers in Java.

Quantifiers

Description

X*

Zero or more occurrences of X

X?

Zero or One occurrence of X

X+

One or More occurrences of X

X{n}

Exactly 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

Types of Quantifiers in Java

The concept of quantifiers can be divided into three types, that are Greedy, Reluctant, and Possessive.

1. Greedy Quantifier (Default)

By default, quantifiers are Greedy. Greedy quantifiers try to match the longest text that matches a given pattern. Greedy quantifiers work by first reading the entire string before trying any match. If the whole text doesn’t match, remove the last character and try again, repeating the process until a match is found.  

Example:

Java
// Java Program to demonstrate
// Greedy Quantifiers
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
// Driver Class
class Geeks
{
    public static void main(String[] args)
    {
        // Making an instance of Pattern class
        // By default quantifier "+" is Greedy
        Pattern p = Pattern.compile("g+");
 
        // Making an instance of Matcher class
        Matcher m = p.matcher("ggg");
 
        while (m.find()){
            System.out.println("Pattern found from " + m.start() +
                               " to " + (m.end()-1));
        }
    }
}

Output
Pattern found from 0 to 2

Explanation: In the above example, the pattern g+ means one or more occurrences of g. Text is ggg. The greedy matcher would match the longest text even if parts of the matching text also match. In this example, g and gg also match, but the greedy matcher produces ggg.

2. Reluctant Quantifier (Appending ? after a quantifier) 

This quantifier uses the approach that is the opposite of greedy quantifiers. It starts with the first character and processes one character at a time. 

Example:

Java
// Java Program to demonstrate
// Reluctant Quantifiers
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Geeks
{
    public static void main(String[] args)
    {
        // Making an instance of Pattern class
        // Here "+" is a Reluctant quantifier because
        // a "?' is appended after it.
        Pattern p = Pattern.compile("g+?");
 
        // Making an instance of Matcher class
        Matcher m = p.matcher("ggg");
 
        while (m.find()){
            System.out.println("Pattern found from " + m.start() +
                               " to " + (m.end()-1));
        }
    }
}

Output
Pattern found from 0 to 0
Pattern found from 1 to 1
Pattern found from 2 to 2

Explanation: In the above example, since the quantifier is reluctant, it matches the shortest part of the test with the pattern. It processes one character at a time.

3. Possessive Quantifier (Appending + after a quantifier) 

This quantifier matches as many characters as possible, like a greedy quantifier. But if the entire string doesn’t match, then it doesn’t try removing characters from the end.

Example:

Java
// Java program to demonstrate
// Possessive Quantifiers
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class Geeks
{
    public static void main(String[] args)
    {
        // Making an instance of Pattern class
        // Here "+" is a Possessive quantifier because
        // a "+' is appended after it.
        Pattern p = Pattern.compile("g++");
 
        // Making an instance of Matcher class
        Matcher m = p.matcher("ggg");
 
        while (m.find()){
            System.out.println("Pattern found from " + m.start() +
                               " to " + (m.end()-1)); 
        }
    }
}

Output
Pattern found from 0 to 2

Explanation: In the above example, we get the same output as Greedy because the whole text matches the pattern.

Greedy vs Possessive Quantifiers

There are some differences between Greedy and Possessive Quantifiers as mentioned below:

Aspect

Greedy Qualifiers

Possessive Quantifiers

Matching Behaviour

Matches as much as possible but allows backtracking if necessary

Matches as much as possible without backtracking

Backtracking

Allows backtracking

Doesn’t need backtracking

Usage

Default quantifiers(* , + , {}) are greedy

Add a + after the quantifier( *+ , ++ , {n,m}+).

Performance

Can be slower due to backtracking

Can be faster, but may fail to match in some cases.

Example:

Java
// Java program to demonstrate difference 
// between Possessive and Greedy Quantifiers
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
class Geeks
{
    public static void main(String[] args)
    {
        // Create a pattern with Greedy quantifier
        Pattern pg = Pattern.compile("g+g");

        // Create same pattern with possessive quantifier
        Pattern pp = Pattern.compile("g++g");         

        System.out.println("Using Greedy Quantifier");
        Matcher mg = pg.matcher("ggg"); 
      
        while (mg.find()){
            System.out.println("Pattern found from " + mg.start() +
                               " to " + (mg.end()-1)); 
        }
          
        System.out.println("\nUsing Possessive Quantifier");
        Matcher mp = pp.matcher("ggg"); 
        
          while (mp.find()){
            System.out.println("Pattern found from " + mp.start() +
                               " to " + (mp.end()-1)); 
        }

    }
}

Output
Using Greedy Quantifier
Pattern found from 0 to 2

Using Possessive Quantifier

Explanation: In the above example, since the first quantifier is greedy, g+ matches the whole string. If we match g+ with whole string, g+g doesn’t match, the Greedy quantifier removes the last character, matches gg with g+, and finds a match. In the Possessive quantifier, we start like Greedy. g+ matches the whole string, but matching g+ with the whole string doesn’t match g+g with ggg. Unlike Greedy, since quantifier is possessive, we stop at this point.


Start your Java programming journey today with our Java Programming Online Course, designed for both beginners and advanced learners. With self-paced lessons covering everything from basic syntax to advanced concepts, you’ll gain the skills needed to excel in the world of programming.

Take the Three 90 Challenge! Complete 90% of the course in 90 days, and earn a 90% refund. Track your progress and stay motivated as you master Java.
Join now and start your path to becoming a Java expert!


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg