Tuesday, 9 January 2018

How to use index methods of matcher class | Java Regex | Java Regular Expressions | Regex in java


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

RegexDemo.java

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

/**
 *
 * Index Methods
 *
 */

public class RegexDemo
{

    public static void main(String[] args)
    {
        Pattern pattern = Pattern.compile("dog");
        Matcher matcher = pattern.matcher("This dog is mine");

        while (matcher.find())
        {

            /*
             * Index methods provide useful index values that show
             * precisely where the match was found in the input String
             * . In the following test, we will confirm the start and
             * end indices of the match for dog in the input String.
             */

            System.out.println("Starting position = " + matcher.start());
            System.out.println("Ending position = " + matcher.end());
        }
    }

}


Output

Starting position = 5
Ending position = 8

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/07583a7ceb95d30e4d41a36362b049aa5d1ec520/BasicJava/RegexDemo_index_methods/?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