Java Matcher start() Method

This method is used to return the start index of the previous match. To return the offset of first matched character, we need to use the find method to match the pattern character by character.

Signature

There are 3 types of start method in java. The signature of the start methods is given below.

No.MethodMethod Name
1int start()This method returns the offset after the matching of the first character.
2int start(int group)This method returns the offset by matching the starting character of the subsequence captured by the given group.
3int start(String name)This method returns the offset after matching of the starting character of the subsequence captured by the given name of the group.

Specified by:

start in interface MatchResult

Returns:

The index of the first character matched

Throws:

IllegalStateException- If no match has yet been attempted, or if the previous match operation failed

Example 1

Output:

Start position :1, End position : 4
Start position :4, End position : 7
Start position :7, End position : 10

Example 2

Output:

start position of Matching String java 2

Example 3

Output:

Start position of matching 5