Matcher region(int, int) method in Java with Examples
The region(int, int) method of Matcher Class restricts the region to be matched by the pattern. This region must be lesser than or same as the previous region, but not greater. Else it would lead to IndexOutOfBoundsException. This method returns a Matcher with the new matching region.
public Matcher region(int startIndex, int endIndex)
Parameters: This method takes two parameters:
- startIndex which is the starting index of the new contrained region.
- endIndex which is the ending index of the new contrained region.
Return Value: This method returns a Matcher with the region, to be matched, contrained.
Exception: This method throws IndexOutOfBoundsException if:
- startIndex or endIndex is less than zero,
- startIndex or endIndex is greater than the length of the input sequence,
- startIndex is greater than endIndex.
Below examples illustrate the Matcher.region() method:
Before changing region, Groups found are: Geeks Geeks Geeks Geeks After changing region, Groups found are: Geeks
Before changing region, Groups found are: FGF FGF After changing region, Groups found are: FGF
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.