📜  Java中的模式类

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

Java中的模式类

支持正则表达式(Regex)的两个类如下,Pattern Class 和 Matcher Class。这两个类一起工作。用简单的语言,我们可以把它们想象成,如果我们想定义一个模式(正则表达式),那么我们使用Pattern 类,如果我们想将该模式与任何其他序列匹配,那么我们使用Matcher 类

模式类

如下图所示, Pattern Class 属于Java.util.regex (包),包属于Java.base (模块)。模式类没有定义构造函数。那么,让我们看看如何创建模式。该模式是使用compile()工厂方法创建的。

句法:

static Pattern compile(String pattern)

在这里,compile 方法中的模式是String 。然后这个字符串(模式)被转换成一个模式,现在可以被 Matcher 类用于模式匹配。模式匹配后,它返回一个包含该模式的Pattern对象。此处创建的Pattern对象现在将用于创建一个 匹配器 Matcher 是通过调用matcher()方法创建的 Pattern定义。

句法:

Matcher matcher(CharSequence x)

这里, x是匹配模式的字符序列。由于这是您正在接受的输入,因此也称为输入序列。 CharSequence是一个提供对字符集的只读访问的接口。

示例 1:

Java
// Java Program to Demonstrate Pattern Class
 
// Importing required classes
import java.util.regex.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a pattern
        Pattern pattern = Pattern.compile("GeeksforGeeks");
 
        // Creating a matcher for the input
        Matcher matcher = pattern.matcher("GeeksforGeeks");
 
        // Checking for a match
        // using matches() method
        boolean letsCheck = matcher.matches();
 
        // Display message only
        System.out.println(
            " Let us check whether the pattern matches or not:");
 
        // Condition check whether pattern is matched or not
        if (letsCheck)
 
            // Matched
            System.out.println("Pattern Matched");
        else
 
            // Not matched
            System.out.println("Pattern does not match");
    }
}


Java
// Java Program to Demonstrate Pattern Class
// via usage of find() method
 
// Importing required classes
import java.util.regex.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a pattern
        Pattern pattern = Pattern.compile("GeeksforGeeks");
 
        // Creating a matcher for the input
        Matcher matcher = pattern.matcher(
            "GFG stands for GeeksforGeeks");
 
        // Display message only
        System.out.println(
            "Checking for GFG in GeeksforGeeks: ");
 
        // Determining if the input sequence contains the
        // subsequence that matches the pattern
        // using find() method
        if (matcher.find())
 
            // Found
            System.out.println("subsequence GFG found");
        else
 
            // Not found
            System.out.println("subsequence GFG not found");
    }
}



输出
Let us check whether the pattern matches or not:
Pattern Matched

示例 2:

Java

// Java Program to Demonstrate Pattern Class
// via usage of find() method
 
// Importing required classes
import java.util.regex.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a pattern
        Pattern pattern = Pattern.compile("GeeksforGeeks");
 
        // Creating a matcher for the input
        Matcher matcher = pattern.matcher(
            "GFG stands for GeeksforGeeks");
 
        // Display message only
        System.out.println(
            "Checking for GFG in GeeksforGeeks: ");
 
        // Determining if the input sequence contains the
        // subsequence that matches the pattern
        // using find() method
        if (matcher.find())
 
            // Found
            System.out.println("subsequence GFG found");
        else
 
            // Not found
            System.out.println("subsequence GFG not found");
    }
}


输出
Checking for GFG in GeeksforGeeks: 
subsequence GFG found