Using Matcher.start(int) Method

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Using Matcher.start(int) Method

This Java example program demonstrates the usage of the Matcher.start(int) method.

[edit] Java Source Code

  • Package: example.matcher
  • File: MatcherStartParamExample.java
package example.matcher;
 
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);
 
    }
 
}

[edit] What Result You Can Get

Run the program, you will get:

My name is Bond. James Bond.
          ^11
My name is Bond. James Bond.
           ^12
My name is Bond. James Bond.
                      ^23
My name is Bond. James Bond.
                       ^24

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.

http://www.ase2009.com/ online casino 132 http://www.hellzyea.com/health health insurance 8DDD http://www.yourautoinsurancesite.com/ auto insurance quotes 135811 http://www.makemeasammich.com/ auto insurance mcnxu

[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools