Java-Quantifier Plus Sign

Quantifier Plus Sign

A quantifier following a subsequence of a pattern determines the possibilities for how that subsequence of a pattern can repeat. Let’s take an example. Suppose you want to find specific character in a string. If you take the simplest case, we can say an character is an arbitrary sequence of one or more characters. The quantifier for one or more is the meta-character “+”. For example, the following program uses the + quantifier to match any arbitrarily long sequence of ‘A’s. The output shows, the regular expression pattern “A+” matches any arbitrarily long sequence of ‘A’s.

Program

Program Source

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Javaapp {
  
    public static void main(String[] args) {
        
        Pattern pat = Pattern.compile("A+");
        Matcher mat = pat.matcher("A2AA3AAA4AAAA5");
        
        int i = 0;
        while(mat.find())
        {
            i++;
            System.out.println(i+"th subsequence  : "+mat.group());
        }
    }
}

Leave a Comment