Documentation

The Java™ Tutorials
Hide TOC
Methods of the Pattern Class模式类的方法
Trail: Essential Java Classes
Lesson: Regular Expressions

Methods of the Pattern Class模式类的方法

Until now, we've only used the test harness to create Pattern objects in their most basic form. 到目前为止,我们只使用测试工具以最基本的形式创建Pattern对象。This section explores advanced techniques such as creating patterns with flags and using embedded flag expressions. 本节探讨高级技术,例如使用标志创建模式和使用嵌入式标志表达式。It also explores some additional useful methods that we haven't yet discussed.它还探索了一些我们尚未讨论的其他有用方法。

Creating a Pattern with Flags创建带有标志的模式

The Pattern class defines an alternate compile method that accepts a set of flags affecting the way the pattern is matched. Pattern类定义了一个替代compile方法,该方法接受一组影响模式匹配方式的标志。The flags parameter is a bit mask that may include any of the following public static fields:flags参数是一个位掩码,可包括以下任何公共静态字段:

In the following steps we will modify the test harness, RegexTestHarness.java to create a pattern with case-insensitive matching.在以下步骤中,我们将修改测试工具RegexTestHarness.java,以创建具有不区分大小写匹配的模式。

First, modify the code to invoke the alternate version of compile:首先,修改代码以调用compile的备用版本:

Pattern pattern = 
Pattern.compile(console.readLine("%nEnter your regex: "),
Pattern.CASE_INSENSITIVE);

Then compile and run the test harness to get the following results:然后编译并运行测试线束以获得以下结果:

Enter your regex: dog
Enter input string to search: DoGDOg
I found the text "DoG" starting at index 0 and ending at index 3.
I found the text "DOg" starting at index 3 and ending at index 6.

As you can see, the string literal "dog" matches both occurences, regardless of case. 如您所见,字符串文字“dog”匹配这两种情况,不管大小写如何。To compile a pattern with multiple flags, separate the flags to be included using the bitwise OR operator "|". 要使用多行标志编译模式,请分隔标志以包含,方法是使用按位或运算符|For clarity, the following code samples hardcode the regular expression instead of reading it from the Console:为清楚起见,以下代码示例对正则表达式进行硬编码,而不是从控制台读取:

pattern = Pattern.compile("[az]$", Pattern.MULTILINE | Pattern.UNIX_LINES);

You could also specify an int variable instead:也可以指定一个int变量:

final int flags = Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
Pattern pattern = Pattern.compile("aa", flags);

Embedded Flag Expressions嵌入的标志表达式

It's also possible to enable various flags using embedded flag expressions. 还可以使用嵌入的标志表达式启用各种标志。Embedded flag expressions are an alternative to the two-argument version of compile, and are specified in the regular expression itself. 嵌入式标志表达式是compile的两参数版本的替代,并在正则表达式本身中指定。The following example uses the original test harness, RegexTestHarness.java with the embedded flag expression (?i) to enable case-insensitive matching.下面的示例使用原始测试工具RegexTestHarness.java和嵌入的标志表达式(?i)来启用不区分大小写的匹配。

Enter your regex: (?i)foo
Enter input string to search: FOOfooFoOfoO
I found the text "FOO" starting at index 0 and ending at index 3.
I found the text "foo" starting at index 3 and ending at index 6.
I found the text "FoO" starting at index 6 and ending at index 9.
I found the text "foO" starting at index 9 and ending at index 12.

Once again, all matches succeed regardless of case.再一次,无论情况如何,所有匹配都会成功。

The embedded flag expressions that correspond to Pattern's publicly accessible fields are presented in the following table:下表显示了与Pattern的公共可访问字段相对应的嵌入式标志表达式:

Constant常数 Equivalent Embedded Flag Expression等价嵌入标志表达式
Pattern.CANON_EQ None
Pattern.CASE_INSENSITIVE (?i)
Pattern.COMMENTS (?x)
Pattern.MULTILINE (?m)
Pattern.DOTALL (?s)
Pattern.LITERAL None
Pattern.UNICODE_CASE (?u)
Pattern.UNIX_LINES (?d)

Using the matches(String,CharSequence) Method使用matches(String,CharSequence)方法

The Pattern class defines a convenient matches method that allows you to quickly check if a pattern is present in a given input string. Pattern类定义了一个方便的matches方法,允许您快速检查给定输入字符串中是否存在模式。As with all public static methods, you should invoke matches by its class name, such as Pattern.matches("\\d","1");. 与所有公共静态方法一样,您应该通过其类名调用matches,例如Pattern.matches("\\d","1");In this example, the method returns true, because the digit "1" matches the regular expression \d.在本例中,该方法返回true,因为数字“1”与正则表达式\d匹配。

Using the split(String) Method使用split(String)方法

The split method is a great tool for gathering the text that lies on either side of the pattern that's been matched. split方法是收集匹配模式两侧的文本的一个很好的工具。As shown below in SplitDemo.java, the split method could extract the words "one two three four five" from the string "one:two:three:four:five":SplitDemo.java中所示,split方法可以从字符串"one:two:three:four:five"中提取单词"one""two""three""four""five"

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class SplitDemo {

    private static final String REGEX = ":";
    private static final String INPUT =
        "one:two:three:four:five";
    
    public static void main(String[] args) {
        Pattern p = Pattern.compile(REGEX);
        String[] items = p.split(INPUT);
        for(String s : items) {
            System.out.println(s);
        }
    }
}
OUTPUT:

one
two
three
four
five

For simplicity, we've matched a string literal, the colon (:) instead of a complex regular expression. 为简单起见,我们匹配了字符串文字,冒号(:)而不是复杂的正则表达式。Since we're still using Pattern and Matcher objects, you can use split to get the text that falls on either side of any regular expression. 因为我们仍然使用PatternMatcher对象,所以可以使用拆分来获取任何正则表达式两侧的文本。Here's the same example, SplitDemo2.java, modified to split on digits instead:下面是相同的示例SplitDemo2.java,改为按数字拆分:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class SplitDemo2 {

    private static final String REGEX = "\\d";
    private static final String INPUT =
        "one9two4three7four1five";

    public static void main(String[] args) {
        Pattern p = Pattern.compile(REGEX);
        String[] items = p.split(INPUT);
        for(String s : items) {
            System.out.println(s);
        }
    }
}
OUTPUT:

one
two
three
four
five

Other Utility Methods其他实用方法

You may find the following methods to be of some use as well:您可能会发现以下方法也有一些用处:

Pattern Method Equivalents in java.lang.Stringjava.lang.String中的模式方法等价物

Regular expression support also exists in java.lang.String through several methods that mimic the behavior of java.util.regex.Pattern. 通过模拟java.util.regex.Pattern行为的几种方法,java.lang.String中也存在正则表达式支持。For convenience, key excerpts from their API are presented below.为方便起见,下面提供了API的关键摘录。

There is also a replace method, that replaces one CharSequence with another:还有一种替换方法,可以用一个CharSequence替换另一个:


Previous page: Boundary Matchers
Next page: Methods of the Matcher Class