📜  Java中的模式 compile(String,int) 方法和示例

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

Java中的模式 compile(String,int) 方法和示例

Pattern类的compile(String, int)方法用于借助标志从正则表达式创建模式,其中表达式和标志都作为参数传递给方法。 Pattern 类包含一个标志列表(int 常量),这些标志有助于使 Pattern 匹配以某些方式表现。例如,标志名称 CASE_INSENSITIVE 用于在匹配时忽略文本的大小写。
句法:

public static Pattern compile(String regex, int flags)

参数:此方法接受两个参数:

  • regex :此参数表示编译成模式的给定正则表达式。
  • flag :此参数是一个表示匹配标志的整数,一个位掩码,可能包括 CASE_INSENSITIVE、MULTILINE、DOTALL、UNICODE_CASE、CANON_EQ、UNIX_LINES、LITERAL、UNICODE_CHARACTER_CLASS 和 COMMENTS。

返回值:此方法返回从传递的正则表达式和标志编译的模式。
异常:此方法抛出以下异常:

  • PatternSyntaxException:如果表达式的语法无效,则会引发此异常。
  • IllegalArgumentException:如果在 flags 中设置了与定义的匹配标志对应的位值以外的位值,则会引发此异常。

下面的程序说明了 compile(String, int) 方法:
方案一:

Java
// 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 = "(.*)(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);
 
        // check whether Regex string is
        // found in actualString or not
        boolean matches = pattern
                              .matcher(actualString)
                              .matches();
 
        System.out.println("actualString "
                           + "contains REGEX = "
                           + matches);
    }
}


Java
// 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 = ".*org.*";
 
        // create the string
        // in which you want to search
        String actualString
            = "geeksforgeeks.org";
 
        // compile the regex to create pattern
        // using compile() method
        Pattern pattern = Pattern.compile(REGEX,
                             Pattern.CASE_INSENSITIVE);
 
        // check whether Regex string is
        // found in actualString or not
        boolean matches = pattern
                              .matcher(actualString)
                              .matches();
 
        System.out.println("actualString "
                           + "contains REGEX = "
                           + matches);
    }
}


输出:
actualString contains REGEX = false

方案二:

Java

// 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 = ".*org.*";
 
        // create the string
        // in which you want to search
        String actualString
            = "geeksforgeeks.org";
 
        // compile the regex to create pattern
        // using compile() method
        Pattern pattern = Pattern.compile(REGEX,
                             Pattern.CASE_INSENSITIVE);
 
        // check whether Regex string is
        // found in actualString or not
        boolean matches = pattern
                              .matcher(actualString)
                              .matches();
 
        System.out.println("actualString "
                           + "contains REGEX = "
                           + matches);
    }
}
输出:
actualString contains REGEX = true

参考:
https://docs.oracle.com/javase/10/docs/api/java Java Java, int)