📜  Java中的 Matcher region(int, int) 方法及示例

📅  最后修改于: 2022-05-13 01:54:45.117000             🧑  作者: Mango

Java中的 Matcher region(int, int) 方法及示例

Matcher 类region(int, int)方法限制了要被模式匹配的区域。该区域必须小于或等于前一个区域,但不能大于。否则会导致 IndexOutOfBoundsException。此方法返回一个带有新匹配区域的 Matcher。

句法:

public Matcher region(int startIndex, int endIndex)

参数:此方法有两个参数:

  • startIndex是新约束区域的起始索引。
  • endIndex是新约束区域的结束索引。

返回值:此方法返回一个带有区域的匹配器,要匹配,约束。

异常:如果出现以下情况,此方法将引发IndexOutOfBoundsException

  • startIndex 或 endIndex 小于零,
  • startIndex 或 endIndex 大于输入序列的长度,
  • startIndex 大于 endIndex。

下面的示例说明了 Matcher.region() 方法:

示例 1:

// 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());
        }
    }
}
输出:
Before changing region,  Groups found are: 
Geeks
Geeks
Geeks
Geeks

After changing region,  Groups found are: 
Geeks

示例 2:

// 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());
        }
    }
}
输出:
Before changing region,  Groups found are: 
FGF
FGF

After changing region,  Groups found are: 
FGF

参考: https://docs.oracle.com/javase/9/docs/api/ Java/util/regex/Matcher.html#region-int-int-