當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。