📜  Java中的模式编译(字符串)方法与示例

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

Java中的模式编译(字符串)方法与示例

Java中Pattern类的compile(String)方法用于从作为参数传递给方法的正则表达式创建模式。每当您需要多次将文本与正则表达式模式匹配时,请使用 Pattern.compile() 方法创建一个 Pattern 实例。
句法:

public static Pattern compile(String regex)

参数:此方法接受一个参数正则表达式,它表示编译成模式的给定正则表达式。
返回值:此方法返回从作为参数传递给方法的正则表达式编译的模式。
异常:此方法抛出以下异常:

  • PatternSyntaxException:如果表达式的语法无效,则抛出此异常。

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

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 = ".*www.*";
 
        // creare the string
        // in which you want to search
        String actualString
            = "www.geeksforgeeks.org";
 
        // compile the regex to create pattern
        // using compile() method
        Pattern pattern = Pattern.compile(REGEX);
 
        // get a matcher object from pattern
        Matcher matcher = pattern.matcher(actualString);
 
        // check whether Regex string is
        // found in actualString or not
        boolean matches = matcher.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 = "brave";
 
        // creare the string
        // in which you want to search
        String actualString
            = "Cat is cute";
 
        // compile the regex to create pattern
        // using compile() method
        Pattern pattern = Pattern.compile(REGEX);
 
        // 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

方案二:

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 = "brave";
 
        // creare the string
        // in which you want to search
        String actualString
            = "Cat is cute";
 
        // compile the regex to create pattern
        // using compile() method
        Pattern pattern = Pattern.compile(REGEX);
 
        // 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

参考: https: Java Java.lang.String)