Java Matcher region() Method

The region method of Matcher class is used to limit the matcher's region by specifying the start and end point of the matcher input sequence. This method resets the matcher and then sets the region to start at the index specified by the start parameter and end at the index specified by the end parameter.

Syntax:

Parameter

start - The index to start searching at (inclusive)

end - The index to the end searching at (exclusive)

Returns

this matcher

Throws

IndexOutOfBoundsException - If start or end is less than zero, if the start is greater than the length of the input sequence, if the end is greater than the length of the input sequence, or if start is greater than the end.

Since

1.5

Example 1

Output:

1 Capturing Group, (a*b) Match String end(): 7
2 Capturing Group, (a*b) Match String end(): 14
  java.util.regex.Matcher[pattern=(a*b)(java) region=0,15 lastmatch=aabjava]

Example 2

Output:

1 Capturing Group, (a*b) Match String end(): 7
java.util.regex.Matcher[pattern=(a*b)(java) region=2,10 lastmatch=bjava]