Java-Find and Get Subsequence

Find and Get Subsequence

To determine if a subsequence of the input sequence matches the pattern, use find( ) method. It returns true if there is a matching subsequence and false otherwise. This method can be called repeatedly, allowing it to find all matching subsequences. Each call to find( ) begins where the previous one left off. You can obtain a string containing the last matching sequence by calling group( ) method. The matching string is returned. If no match exists, then null is returned.

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("Java");
        Matcher mat = pat.matcher("Java5Java6Java7Java8");
        
        int i = 0;
        while(mat.find())
        {
            i++;
            System.out.println(i+"th subsequence : "+mat.group());
        }
    }
}

Leave a Comment