当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Matcher region(int, int)用法及代码示例


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

用法:

public Matcher region(int startIndex, int endIndex)

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


  • startIndex这是新禁区的起始指标。
  • endIndex这是新禁区的结束 index 。

返回值:此方法返回一个Matcher,其中包含要匹配的区域。

异常:在以下情况下,此方法将引发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-



相关用法


注:本文由纯净天空筛选整理自Kirti_Mangal大神的英文原创作品 Matcher region(int, int) method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。