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–