MatcherEndExample.java Source code

Java tutorial

Introduction

Here is the source code for MatcherEndExample.java

Source

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

public class MatcherEndExample {
    public static void main(String args[]) {
        String candidateString = "My name is Bond. James Bond.";
        String matchHelper[] = { "               ^", "                           ^" };
        Pattern p = Pattern.compile("Bond");
        Matcher matcher = p.matcher(candidateString);

        matcher.find();
        int endIndex = matcher.end();
        System.out.println(candidateString);
        System.out.println(matchHelper[0] + endIndex);

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