Java Pattern flags()方法及示例
Java中 Pattern 类的 flags() 方法是用来返回模式的匹配标志的。匹配标志是一个位掩码,可能包括CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE, CANON_EQ, UNIX_LINES, LITERAL, UNICODE_CHARACTER_CLASS和COMMENTS标志。
语法
public int flags()
参数: 该方法不接受任何参数。
返回值: 该方法返回模式的匹配标志。
下面的程序说明了flags()方法。
程序1 :
// Java program to demonstrate// Pattern.flags() method import java.util.regex.*; public class GFG { public static void main(String[] args) { // create a REGEX String String REGEX = "(.*)(for)(.*)?"; // create the string // in which you want to search String actualString = "code of Machine"; // compile the regex to create pattern // using compile() method Pattern pattern = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE); // find the flag of pattern int flag = pattern.flags(); System.out.println("Pattern's match flag = " + flag); }}
输出:
Pattern's match flag = 2
程序2
// Java program to demonstrate// Pattern.compile method import java.util.regex.*; public class GFG { public static void main(String[] args) { // create a REGEX String String REGEX = "(.*)(ee)(.*)?"; // create the string // in which you want to search String actualString = "geeks"; // compile the regex to create pattern // using compile() method Pattern pattern = Pattern.compile(REGEX, Pattern.MULTILINE); // find the flag of pattern int flag = pattern.flags(); System.out.println("Pattern's match flag = " + flag); }}输出:
Pattern's match flag = 8
参考文献:
https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#flags()
