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.

Syntax:

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:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate region() method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the regex to be checked
        String regex = "(Geeks)";
  
        // Create a pattern from regex
        Pattern pattern = Pattern.compile(regex);
  
        // Get the String to be matched
        String stringToBeMatched
            = "GeeksForGeeks Geeks for For Geeks Geek";
  
        // Create a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
  
        System.out.println("Before changing region, "
                           + " Groups found are: ");
  
        while (matcher.find()) {
  
            // Get the group matched using group() method
            System.out.println(matcher.group());
        }
  
        System.out.println("\nAfter changing region, "
                           + " Groups found are: ");
  
        // Restrict the region to 0, 10
        // using region() method
        Matcher matcher1
            = matcher.region(0, 10);
  
        while (matcher1.find()) {
  
            // Get the group matched using group() method
            System.out.println(matcher1.group());
        }
    }
}

chevron_right


Output:

Before changing region,  Groups found are: 
Geeks
Geeks
Geeks
Geeks

After changing region,  Groups found are: 
Geeks

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate region() method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the regex to be checked
        String regex = "(FGF)";
  
        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);
  
        // Get the String to be matched
        String stringToBeMatched
            = "FGF GFG GFG FGF";
  
        // Create a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
  
        System.out.println("Before changing region, "
                           + " Groups found are: ");
  
        while (matcher.find()) {
  
            // Get the group matched using group() method
            System.out.println(matcher.group());
        }
  
        System.out.println("\nAfter changing region, "
                           + " Groups found are: ");
  
        // Restrict the region to 0, 5
        // using region() method
        Matcher matcher1 = matcher.region(0, 5);
  
        while (matcher1.find()) {
  
            // Get the group matched using group() method
            System.out.println(matcher1.group());
        }
    }
}

chevron_right


Output:

Before changing region,  Groups found are: 
FGF
FGF

After changing region,  Groups found are: 
FGF

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/regex/Matcher.html#region-int-int-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.