📜  Java中的 Pattern flags() 方法和示例

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

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() 方法:

方案一:

// 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

方案二:

// 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 Java()