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


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