📌  相关文章
📜  Java中的 Matcher regionEnd() 方法及示例

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

Java中的 Matcher regionEnd() 方法及示例

Matcher 类regionEnd()方法用于获取当前匹配器中的模式要匹配的区域的 endIndex。此方法返回一个整数值,该值是此匹配器区域的 endIndex。

句法:

public int regionEnd()

参数:此方法不带参数。

返回值:此方法返回一个整数值,该值是此匹配器区域的 endIndex。

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

示例 1:

// Java code to illustrate regionEnd() 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);
  
        // Get previous endIndex of region
        // using regionEnd() method
        System.out.println("Before changing region, "
                           + " Region ends from: "
                           + matcher.regionEnd());
  
        // Restrict the region to 0, 10
        matcher = matcher.region(0, 10);
  
        // Get previous endIndex of region
        // using regionEnd() method
        System.out.println("After changing region, "
                           + " Region ends from: "
                           + matcher.regionEnd());
    }
}
输出:
Before changing region,  Region ends from: 38
After changing region,  Region ends from: 10

示例 2:

// Java code to illustrate regionEnd() method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get the regex to be checked
        String regex = "(F*F)";
  
        // Create a pattern from regex
        Pattern pattern = Pattern.compile(regex);
  
        // Get the String to be matched
        String stringToBeMatched
            = "GFGFGFGFGFGFGFGFGFG FGF GFG GFG FGF";
  
        // Create a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);
  
        // Get previous endIndex of region
        // using regionEnd() method
        System.out.println("Before changing region, "
                           + " Region ends from: "
                           + matcher.regionEnd());
  
        // Restrict the region to 0, 5
        matcher = matcher.region(0, 5);
  
        // Get previous endIndex of region
        // using regionEnd() method
        System.out.println("After changing region, "
                           + " Region ends from: "
                           + matcher.regionEnd());
    }
}
输出:
Before changing region,  Region ends from: 35
After changing region,  Region ends from: 5

参考: https://docs.oracle.com/javase/9/docs/api/ Java/util/regex/Matcher.html#regionEnd–