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


Java Matcher requireEnd()用法及代码示例


Matcher类的requireEnd()方法用于检查锚点的任何组合是否导致匹配在最后被限制。这些锚点可以是任何锚点,例如单词锚点或前行。此方法返回一个声明相同的布尔值。

用法:

public boolean requireEnd()

参数:此方法不带参数。


返回值:此方法返回一个布尔值,该值指示锚点的任何组合是否导致匹配最终结束。

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

示例1:

// Java code to illustrate requireEnd() method 
  
import java.util.regex.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the regex to be checked 
        // with an anchor 
        String regex = "Geeks$"; 
  
        // Create a pattern from regex 
        Pattern pattern 
            = Pattern.compile(regex); 
  
        // Get the String to be matched 
        String stringToBeMatched 
            = "GFG GFG GEEKS Geeks"; 
  
        // Create a matcher for the input String 
        Matcher matcher 
            = pattern.matcher(stringToBeMatched); 
  
        matcher.find(); 
  
        // Check if a match has been found 
        // using requireEnd() method 
        System.out.println("Has any anchor "
                           + "bounded the search: "
                           + matcher.requireEnd()); 
    } 
}
输出:
Has any anchor bounded the search: true

示例2:

// Java code to illustrate requireEnd() method 
  
import java.util.regex.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the regex to be checked 
        // without any anchor 
        String regex = "Geeks"; 
  
        // Create a pattern from regex 
        Pattern pattern 
            = Pattern.compile(regex); 
  
        // Get the String to be matched 
        String stringToBeMatched 
            = "GFG GFG GEEKS Geeks"; 
  
        // Create a matcher for the input String 
        Matcher matcher 
            = pattern.matcher(stringToBeMatched); 
  
        matcher.find(); 
  
        // Check if a match has been found 
        // using requireEnd() method 
        System.out.println("Has any anchor "
                           + "bounded the search: "
                           + matcher.requireEnd()); 
    } 
}
输出:
Has any anchor bounded the search: false

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



相关用法


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