MatcherStartParamExample.java Source code

Java tutorial

Introduction

Here is the source code for MatcherStartParamExample.java

Source

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

public class MatcherStartParamExample {
    public static void main(String args[]) {
        Pattern p = Pattern.compile("B(ond)");

        String candidateString = "My name is Bond. James Bond.";
        String matchHelper[] = { "          ^", "           ^", "                      ^",
                "                       ^" };
        Matcher matcher = p.matcher(candidateString);
        matcher.find();
        int startIndex = matcher.start(0);
        System.out.println(candidateString);
        System.out.println(matchHelper[0] + startIndex);

        int nextIndex = matcher.start(1);
        System.out.println(candidateString);
        System.out.println(matchHelper[1] + nextIndex);

        matcher.find();
        startIndex = matcher.start(0);
        System.out.println(candidateString);
        System.out.println(matchHelper[2] + startIndex);

        nextIndex = matcher.start(1);
        System.out.println(candidateString);
        System.out.println(matchHelper[3] + nextIndex);

    }

}