📜  Java中的正则表达式

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

Java中的正则表达式

JavaJava搜索、操作和编辑字符串。电子邮件验证和密码是正则表达式广泛用于定义约束的几个字符串区域。正则表达式在Java.util.regex包下提供。这由3 个类和 1 个接口组成。 Java.util.regex包主要由以下三个类组成,如下表所示:

S. No.Class/InterfaceDescription
1.Pattern ClassUsed for defining patterns
2.Matcher ClassUsed for performing match operations on text using patterns
3.PatternSyntaxException ClassUsed for indicating syntax error in a regular expression pattern
4.MatchResult InterfaceUsed for representing the result of a match operation

Java中的 Regex 为我们提供了3 个类和 1 个接口,如下所示:

  1. 模式类
  2. 匹配器类
  3. PatternSyntaxException 类
  4. 匹配结果接口

更多的理解可以从下面提供的图片解释如下:

Java 正则表达式

第一类:模式类

此类是正则表达式的汇编,可用于定义各种类型的模式,不提供公共构造函数。这可以通过调用 compile() 方法来创建,该方法接受正则表达式作为第一个参数,因此在执行后返回一个模式。

S. No.MethodDescription
1.compile(String regex)It is used to compile the given regular expression into a pattern.
2.compile(String regex, int flags)It is used to compile the given regular expression into a pattern with the given flags.
3.flags()It is used to return this pattern’s match flags.
4.matcher(CharSequence input)It is used to create a matcher that will match the given input against this pattern.
5.matches(String regex, CharSequence input)It is used to compile the given regular expression and attempts to match the given input against it.
6.pattern()It is used to return the regular expression from which this pattern was compiled.
7.quote(String s)It is used to return a literal pattern String for the specified String.
8.split(CharSequence input)It is used to split the given input sequence around matches of this pattern.
9.split(CharSequence input, int limit)It is used to split the given input sequence around matches of this pattern. The limit parameter controls the number of times the pattern is applied.
10.toString()It is used to return the string representation of this pattern.

示例:模式类

Java
// Java Program Demonstrating Working of matches() Method
// Pattern class
 
// Importing Pattern class from java.util.regex package
import java.util.regex.Pattern;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Following line prints "true" because the whole
        // text "geeksforgeeks" matches pattern
        // "geeksforge*ks"
        System.out.println(Pattern.matches(
            "geeksforge*ks", "geeksforgeeks"));
 
        // Following line prints "false" because the whole
        // text "geeksfor" doesn't match pattern "g*geeks*"
        System.out.println(
            Pattern.matches("g*geeks*", "geeksfor"));
    }
}


Java
// Java program to demonstrate working of
// String matching in Java
 
// Importing Matcher and Pattern class
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Create a pattern to be searched
        // Custom pattern
        Pattern pattern = Pattern.compile("geeks");
 
        // Search above pattern in "geeksforgeeks.org"
        Matcher m = pattern.matcher("geeksforgeeks.org");
 
        // Finding string using find() method
        while (m.find())
 
            // Print starting and ending indexes
            // of the pattern in the text
            // using this functionality of this class
            System.out.println("Pattern found from "
                               + m.start() + " to "
                               + (m.end() - 1));
    }
}


Java
// Java program to demonstrate working of
// String matching in Java
 
// Importing Matcher and Pattern class
// from java.util package
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating a pattern to be searched
        // Custom pattern to be searched
        Pattern pattern = Pattern.compile("ge*");
 
        // Searching for the above pattern in
        // "geeksforgeeks.org"
        Matcher m = pattern.matcher("geeksforgeeks.org");
 
        // Checking whether the pattern is there or not
        // using find() method
        while (m.find())
 
            // Print starting and ending indexes of the
            // pattern in text using method functionality of
            // this class
            System.out.println("Pattern found from "
                               + m.start() + " to "
                               + (m.end() - 1));
    }
}


Java
// Java Program Demonstrating Working of String matching
 
// Importing Matcher class and Pattern classes
// from java.util.regex package
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating a pattern to be searched
        Pattern pattern = Pattern.compile(
            "ge*", Pattern.CASE_INSENSITIVE);
 
        // Searching above pattern in "geeksforgeeks.org"
        Matcher m = pattern.matcher("GeeksforGeeks.org");
 
        // Find th above string using find() method
        while (m.find())
 
            // Printing the starting and ending indexes of
            // the pattern in text using class method
            // functionalities
            System.out.println("Pattern found from "
                               + m.start() + " to "
                               + (m.end() - 1));
    }
}


Java
// Java program Illustrating Working of split() Method
// by Splitting a text by a given pattern
 
// Importing Matcher and Pattern classes from
// java.util.regex package
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Custom string
        String text = "geeks1for2geeks3";
 
        // Specifies the string pattern
        // which is to be searched
        String delimiter = "\\d";
        Pattern pattern = Pattern.compile(
            delimiter, Pattern.CASE_INSENSITIVE);
 
        // Used to perform case insensitive search of the
        // string
        String[] result = pattern.split(text);
 
        // Iterating using for each loop
        for (String temp : result)
            System.out.println(temp);
    }
}


输出
true
false

第 2 类:匹配器类

该对象用于对Java中的输入字符串执行匹配操作,从而解释前面解释的模式。这也没有定义公共构造函数。这可以通过在任何模式对象上调用 matcher() 来实现。

S. No.MethodDescription
1.find()It is mainly used for searching multiple occurrences of the regular expressions in the text.
2.find(int start)It is used for searching occurrences of the regular expressions in the text starting from the given index.
3.start()It is used for getting the start index of a match that is being found using find() method.
4.end()It is used for getting the end index of a match that is being found using find() method. It returns the index of the character next to the last matching character.
5.groupCount()It is used to find the total number of the matched subsequence.
6.group()It is used to find the matched subsequence.
7.matches()It is used to test whether the regular expression matches the pattern.

让我们像讨论 Pattern 类一样讨论一些示例程序。在这里,我们将讨论一些演示 compile()、find()、start()、end() 和 split() 工作原理的Java程序,以便更好地理解 Matcher 类。

示例 1:模式搜索

Java

// Java program to demonstrate working of
// String matching in Java
 
// Importing Matcher and Pattern class
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Create a pattern to be searched
        // Custom pattern
        Pattern pattern = Pattern.compile("geeks");
 
        // Search above pattern in "geeksforgeeks.org"
        Matcher m = pattern.matcher("geeksforgeeks.org");
 
        // Finding string using find() method
        while (m.find())
 
            // Print starting and ending indexes
            // of the pattern in the text
            // using this functionality of this class
            System.out.println("Pattern found from "
                               + m.start() + " to "
                               + (m.end() - 1));
    }
}
输出
Pattern found from 0 to 4
Pattern found from 8 to 12

示例 2:简单的正则表达式搜索

Java

// Java program to demonstrate working of
// String matching in Java
 
// Importing Matcher and Pattern class
// from java.util package
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating a pattern to be searched
        // Custom pattern to be searched
        Pattern pattern = Pattern.compile("ge*");
 
        // Searching for the above pattern in
        // "geeksforgeeks.org"
        Matcher m = pattern.matcher("geeksforgeeks.org");
 
        // Checking whether the pattern is there or not
        // using find() method
        while (m.find())
 
            // Print starting and ending indexes of the
            // pattern in text using method functionality of
            // this class
            System.out.println("Pattern found from "
                               + m.start() + " to "
                               + (m.end() - 1));
    }
}
输出
Pattern found from 0 to 2
Pattern found from 8 to 10
Pattern found from 16 to 16

示例 3:不区分大小写的模式搜索 

Java

// Java Program Demonstrating Working of String matching
 
// Importing Matcher class and Pattern classes
// from java.util.regex package
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating a pattern to be searched
        Pattern pattern = Pattern.compile(
            "ge*", Pattern.CASE_INSENSITIVE);
 
        // Searching above pattern in "geeksforgeeks.org"
        Matcher m = pattern.matcher("GeeksforGeeks.org");
 
        // Find th above string using find() method
        while (m.find())
 
            // Printing the starting and ending indexes of
            // the pattern in text using class method
            // functionalities
            System.out.println("Pattern found from "
                               + m.start() + " to "
                               + (m.end() - 1));
    }
}
输出
Pattern found from 0 to 2
Pattern found from 8 to 10
Pattern found from 16 to 16

示例 4: split() 方法根据分隔符模式拆分文本。

字符串 split() 方法在给定正则表达式的匹配项周围打破给定字符串。此方法存在两种变体,因此在开始实施此方法之前请先了解一下。

插图:

Input  --> String: 016-78967
Output -->  Regex: {"016", "78967"}

Java

// Java program Illustrating Working of split() Method
// by Splitting a text by a given pattern
 
// Importing Matcher and Pattern classes from
// java.util.regex package
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Custom string
        String text = "geeks1for2geeks3";
 
        // Specifies the string pattern
        // which is to be searched
        String delimiter = "\\d";
        Pattern pattern = Pattern.compile(
            delimiter, Pattern.CASE_INSENSITIVE);
 
        // Used to perform case insensitive search of the
        // string
        String[] result = pattern.split(text);
 
        // Iterating using for each loop
        for (String temp : result)
            System.out.println(temp);
    }
}
输出
geeks
for
geeks

现在我们完成了对这两个类的讨论。现在我们将向您介绍两个非常清楚的新概念还有一个异常类与

第 3 类:PatternSyntaxException 类

这是 Regex 的一个对象,用于指示正则表达式模式中的语法错误,并且是未经检查的异常。以下是以表格格式提供的 PatternSyntaxException 类中的方法,如下所示。

S. No.MethodDescription
1.getDescription()It is used to retrieve the description of the error.
2.getIndex()It is used to retrieve the error index.
3.getMessage()It is used to return a multi-line string containing the description of the syntax error and its index, the erroneous regular-expression pattern, and a visual indication of the error index within the pattern.
4.getPattern()It is used to retrieve the erroneous regular-expression pattern.

接口一:MatchResult接口

该接口用于确定正则表达式匹配操作的结果。需要注意的是,虽然可以看到匹配边界、组、组边界,但是不允许通过MatchResult进行修改。以下是此界面中的方法,如下表格式提供,如下所示:

S. No.MethodDescription
1.end()It is used to return the offset after the last character is matched.
2.end(int group)It is used to return the offset after the last character of the subsequence captured by the given group during this match.
3.group()It is used to return the input subsequence matched by the previous match.
4.group(int group)It is used to return the input subsequence captured by the given group during the previous match operation.
5.groupCount()It is used to return the number of capturing groups in this match result’s pattern.
6.start()It is used to return the start index of the match.
7.start(int group)It is used to return the start index of the subsequence captured by the given group during this match.

最后,让我们讨论从上述文章中检索到的一些重要观察结果

  1. 我们通过调用 Pattern.compile() 创建一个模式对象,没有构造函数。 compile() 是 Pattern 类中的一个静态方法。
  2. 像上面一样,我们在 Pattern 类的对象上使用 matcher() 创建一个 Matcher 对象。
  3. Pattern.matches() 也是一个静态方法,用于检查给定文本是否作为一个整体匹配模式。
  4. find() 用于查找文本中多次出现的模式。
  5. 我们可以使用 split() 方法根据分隔符模式拆分文本