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


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