Tuesday, 19 December 2017

How to find multiple occurrences using matcher methods | Java Regex


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=UX8EDfu0qX8&list=UUhwKlOVR041tngjerWxVccw

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

/**
 *
 * Example to find out the multiple occurrences using Matcher methods.
 *
 */

public class RegexDemo
{

    public static void main(String[] args)
    {
        String inputCharSeq = "Cat AA Cat AA Cat AAA Cat";
        String regex = "Cat";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(inputCharSeq);

        /*
         * Returns: true if, and only if, a subsequence of the input sequence
         * matches this matcher's pattern
         */

        while (matcher.find())
        {
            System.out.println("Found at: " + matcher.start() + " - " + matcher.end());
        }
    }

}

Output
Found at: 0 - 3
Found at: 7 - 10
Found at: 14 - 17
Found at: 22 - 25

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_find_multiocc.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_find_multiocc

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/ef02c9011c18b0aa670526ba89cc11f493b9b854/BasicJava/RegexDemo_find_multiocc/?at=master

See also:

  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment