Java-Group Another Example

Group Another Example

The parentheses ( and ) are used for expressing a range of choices for multiple characters. Keep in mind that the expression ‘ABC’ will often need to be surrounded in parentheses for it to work the way you desire. For example: ABC+ might seem like it would match the sequence ‘ABC’ one or more times, and if you apply it to the input string ‘ABCABCABC’, you will in fact get three matches. However, the expression actually says, Match ‘AB’ followed by one or more occurrences of ‘C’.

To match the entire string ‘ABC’ one or more times, you must say: (ABC)+.



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("(ABC)+");
        Matcher mat = pat.matcher("ABCCABCABCGABCCCABCD");
        int i = 0;
            System.out.println(i+"th subsequence  : ";

Leave a Comment