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


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



相关用法


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