Java : Pattern (regex) 示例

Pattern (Java SE 22 & JDK 22) 示例。
您将在大多数 Pattern 方法中找到代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

正则表达式的编译表示。 (机器翻译)

Class diagram

// Tests if this pattern matches a given input string.
final var pattern = Pattern.compile("a+bc");
final var matchPredicate = pattern.asMatchPredicate();

System.out.println(matchPredicate.test("abc")); // true
System.out.println(matchPredicate.test("aaaabc")); // true

System.out.println(matchPredicate.test("ab")); // false
System.out.println(matchPredicate.test("abcD")); // false
System.out.println(matchPredicate.test("Zabc")); // false
// Tests if this pattern is found in a given input string.
final var pattern = Pattern.compile("a+bc");
final var predicate = pattern.asPredicate();

System.out.println(predicate.test("abc")); // true
System.out.println(predicate.test("aaaabc")); // true

System.out.println(predicate.test("ab")); // false
System.out.println(predicate.test("abcD")); // true
System.out.println(predicate.test("Zabc")); // true
// An examples with a matcher.
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("xyz aaaabc 012");

if (matcher.find()) {
    System.out.println(matcher.group()); // "aaaabc"
}

Fields

static final int CANON_EQ

实现规范等价。 (机器翻译)

final var regex = "a\u030A";
System.out.println(regex); // å

final var input = "\u00E5";
System.out.println(input); // å

{
    final var pattern = Pattern.compile(regex, Pattern.CANON_EQ);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // true
}

{
    final var pattern = Pattern.compile(regex);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // false
}

static final int CASE_INSENSITIVE

启用不区分大小写的匹配。 (机器翻译)

final var regex = "abc";
final var input = "ABC";

{
    final var pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // true
}

{
    final var pattern = Pattern.compile(regex);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // false
}

static final int COMMENTS

允许模式中存在空格和注释。 (机器翻译)

final var regex = "a b c # xyz";
final var input = "abc";

{
    final var pattern = Pattern.compile(regex, Pattern.COMMENTS);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // true
}

{
    final var pattern = Pattern.compile(regex);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // false
}

static final int DOTALL

启用 dotall 模式。 (机器翻译)

final var regex = "abc..yz";
final var input = "abc\nxyz";

{
    final var pattern = Pattern.compile(regex, Pattern.DOTALL);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // true
}

{
    final var pattern = Pattern.compile(regex);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // false
}

static final int LITERAL

启用模式的文字解析。 (机器翻译)

final var regex = "abc+";
final var input1 = "abc+";
final var input2 = "abccc";

{
    final var pattern = Pattern.compile(regex, Pattern.LITERAL);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input1)); // true
    System.out.println(matchPredicate.test(input2)); // false
}

{
    final var pattern = Pattern.compile(regex);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input1)); // false
    System.out.println(matchPredicate.test(input2)); // true
}

static final int MULTILINE

启用多行模式。 (机器翻译)

final var regex = "^abc$";
final var input1 = """
        012
        abc
        xyz
        """;
final var input2 = "abc";

{
    final var pattern = Pattern.compile(regex, Pattern.MULTILINE);
    final var predicate = pattern.asPredicate();

    System.out.println(predicate.test(input1)); // true
    System.out.println(predicate.test(input2)); // true
}

{
    final var pattern = Pattern.compile(regex);
    final var predicate = pattern.asPredicate();

    System.out.println(predicate.test(input1)); // false
    System.out.println(predicate.test(input2)); // true
}

static final int UNICODE_CASE

启用 Unicode 感知大小写折叠。 (机器翻译)

// Full-width characters.
final var regex = "abc";
final var input = "ABC";

{
    final var pattern = Pattern.compile(regex,
            Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // true
}

{
    final var pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input)); // false
}

static final int UNICODE_CHARACTER_CLASS

启用预定义字符类和 POSIX 字符类的 Unicode 版本。 (机器翻译)

final var regex = "\\p{Lower}+";

// Half-width characters.
final var input1 = "abc";

// Full-width characters.
final var input2 = "abc";

{
    final var pattern = Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input1)); // true
    System.out.println(matchPredicate.test(input2)); // true
}

{
    final var pattern = Pattern.compile(regex);
    final var matchPredicate = pattern.asMatchPredicate();

    System.out.println(matchPredicate.test(input1)); // true
    System.out.println(matchPredicate.test(input2)); // false
}

static final int UNIX_LINES

启用 Unix 行模式。 (机器翻译)

final var regex = "^abc$";

final var input1 = "abc\r\nxyz";
final var input2 = "abc\nxyz";
final var input3 = "abc\rxyz";

{
    final var pattern = Pattern.compile(regex,
            Pattern.UNIX_LINES | Pattern.MULTILINE);
    final var predicate = pattern.asPredicate();

    System.out.println(predicate.test(input1)); // false
    System.out.println(predicate.test(input2)); // true
    System.out.println(predicate.test(input3)); // false
}

{
    final var pattern = Pattern.compile(regex, Pattern.MULTILINE);
    final var predicate = pattern.asPredicate();

    System.out.println(predicate.test(input1)); // true
    System.out.println(predicate.test(input2)); // true
    System.out.println(predicate.test(input3)); // true
}

Methods

Predicate<String> asMatchPredicate ()

创建一个谓词,测试此模式是否与给定的输入字符串匹配。 (机器翻译)

final var pattern = Pattern.compile("a+bc");

final var matchPredicate = pattern.asMatchPredicate();
final var predicate = pattern.asPredicate();

System.out.println(matchPredicate.test("aaabc")); // true
System.out.println(predicate.test("aaabc")); // true

System.out.println(matchPredicate.test("012 abc xyz")); // false
System.out.println(predicate.test("012 abc xyz")); // true

Predicate<String> asPredicate ()

创建一个谓词,测试在给定的输入字符串中是否找到该模式。 (机器翻译)

final var pattern = Pattern.compile("a+bc");

final var matchPredicate = pattern.asMatchPredicate();
final var predicate = pattern.asPredicate();

System.out.println(matchPredicate.test("aaabc")); // true
System.out.println(predicate.test("aaabc")); // true

System.out.println(matchPredicate.test("012 abc xyz")); // false
System.out.println(predicate.test("012 abc xyz")); // true

static Pattern compile (String regex)

将给定的正则表达式编译为一种模式。 (机器翻译)

final var pattern = Pattern.compile("a+bc");
final var matchPredicate = pattern.asMatchPredicate();

System.out.println(matchPredicate.test("abc")); // true
System.out.println(matchPredicate.test("aaaabc")); // true
System.out.println(matchPredicate.test("bc")); // false

static Pattern compile (String regex, int flags)

将给定的正则表达式编译为具有给定标志的模式。 (机器翻译)

final var pattern = Pattern.compile("abc", Pattern.CASE_INSENSITIVE);
final var matchPredicate = pattern.asMatchPredicate();

System.out.println(matchPredicate.test("abc")); // true
System.out.println(matchPredicate.test("ABC")); // true
final var pattern = Pattern.compile("abc");
final var matchPredicate = pattern.asMatchPredicate();

System.out.println(matchPredicate.test("abc")); // true
System.out.println(matchPredicate.test("ABC")); // false

int flags ()

返回此模式的匹配标志。 (机器翻译)

final var pattern = Pattern.compile("abc", Pattern.LITERAL);

System.out.printf("%#x%n", Pattern.LITERAL); // 0x10
System.out.printf("%#x%n", pattern.flags()); // 0x10
final var pattern = Pattern.compile("abc",
        Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

System.out.printf("%#x%n", Pattern.UNICODE_CASE); // 0x40
System.out.printf("%#x%n", Pattern.CASE_INSENSITIVE); // 0x2
System.out.printf("%#x%n", pattern.flags()); // 0x42

Matcher matcher (CharSequence input)

创建一个匹配器,将给定的输入与该模式进行匹配。 (机器翻译)

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("zyz aaaabc 012");

if (matcher.find()) {
    System.out.println(matcher.group()); // "aaaabc"
}

static boolean matches (String regex, CharSequence input)

编译给定的正则表达式并尝试将给定的输入与其匹配。 (机器翻译)

System.out.println(Pattern.matches("a+bc", "aaabc")); // true
System.out.println(Pattern.matches("a+bc", "aaabc xyz")); // false

System.out.println(Pattern.matches("[a-z]+", "abcxyz")); // true
System.out.println(Pattern.matches("[a-z]+", "0123")); // false

Map<String,Integer> namedGroups ()

返回从捕获组名到组号的不可修改的映射。 (机器翻译)

final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
System.out.println(pattern.namedGroups()); // {A=1, B=2}

String pattern ()

返回编译此模式的正则表达式。 (机器翻译)

final var pattern = Pattern.compile("a+bc");
System.out.println(pattern.pattern()); // "a+bc"
final var pattern = Pattern.compile("[a-z]+");
System.out.println(pattern.pattern()); // "[a-z]+"

static String quote (String s)

返回指定字符串的文字模式字符串。 (机器翻译)

final var regex = "a+bc";

final var quote = Pattern.quote(regex);
System.out.println(quote); // "\Qa+bc\E"

final var pattern = Pattern.compile(quote);
final var matchPredicate = pattern.asMatchPredicate();

System.out.println(matchPredicate.test("a+bc")); // true
System.out.println(matchPredicate.test("aaabc")); // false

String[] split (CharSequence input)

围绕此模式的匹配分割给定的输入序列。 (机器翻译)

final var input = "boo:and:foo";

final var pattern = Pattern.compile(":");
final var ret = pattern.split(input);
System.out.println(Arrays.toString(ret)); // [boo, and, foo]
final var input = "boo:and:foo";
final var pattern = Pattern.compile("o");

final var ret = pattern.split(input);
System.out.println(Arrays.toString(ret)); // [b, , :and:f]

String[] split (CharSequence input, int limit)

围绕此模式的匹配分割给定的输入序列。 (机器翻译)

final var input = "boo:and:foo";
final var pattern = Pattern.compile(":");

final var ret1 = pattern.split(input, 2);
System.out.println(Arrays.toString(ret1)); // [boo, and:foo]

final var ret2 = pattern.split(input, 5);
System.out.println(Arrays.toString(ret2)); // [boo, and, foo]

final var ret3 = pattern.split(input, -2);
System.out.println(Arrays.toString(ret3)); // [boo, and, foo]
final var input = "boo:and:foo";
final var pattern = Pattern.compile("o");

final var ret1 = pattern.split(input, 5);
System.out.println(Arrays.toString(ret1)); // [b, , :and:f, , ]

final var ret2 = pattern.split(input, -2);
System.out.println(Arrays.toString(ret2)); // [b, , :and:f, , ]

final var ret3 = pattern.split(input, 0);
System.out.println(Arrays.toString(ret3)); // [b, , :and:f]

Stream<String> splitAsStream (CharSequence input)

围绕此模式的匹配项根据给定的输入序列创建一个流。 (机器翻译)

final var input = "boo:and:foo";
final var pattern = Pattern.compile(":");
final var stream = pattern.splitAsStream(input);

stream.forEach(s -> {
    System.out.println(s);
});

// Result
// ↓
// "boo"
// "and"
// "foo"
final var input = "boo:and:foo";
final var pattern = Pattern.compile("o");
final var stream = pattern.splitAsStream(input);

stream.forEach(s -> {
    System.out.println(s);
});

// Result
// ↓
// "b"
// ""
// ":and:f"

String[] splitWithDelimiters (CharSequence input, int limit)

围绕此模式的匹配拆分给定的输入序列并返回字符串和匹配的分隔符。 (机器翻译)

final var input = "boo:and:foo";
final var pattern = Pattern.compile(":");

final var ret1 = pattern.splitWithDelimiters(input, 2);
System.out.println(Arrays.toString(ret1)); // [boo, :, and:foo]

final var ret2 = pattern.splitWithDelimiters(input, 5);
System.out.println(Arrays.toString(ret2)); // [boo, :, and, :, foo]

final var ret3 = pattern.splitWithDelimiters(input, -2);
System.out.println(Arrays.toString(ret3)); // [boo, :, and, :, foo]
final var input = "boo:and:foo";
final var pattern = Pattern.compile("o");

final var ret1 = pattern.splitWithDelimiters(input, 5);
System.out.println(Arrays.toString(ret1)); // [b, o, , o, :and:f, o, , o, ]

final var ret2 = pattern.splitWithDelimiters(input, -2);
System.out.println(Arrays.toString(ret2)); // [b, o, , o, :and:f, o, , o, ]

final var ret3 = pattern.splitWithDelimiters(input, 0);
System.out.println(Arrays.toString(ret3)); // [b, o, , o, :and:f, o, , o]

String toString ()

返回此模式的字符串表示形式。 (机器翻译)

final var pattern = Pattern.compile("a+bc");
final var str = pattern.toString();
System.out.println(str); // "a+bc"
final var pattern = Pattern.compile("[a-z]+");
final var str = pattern.toString();
System.out.println(str); // "[a-z]+"

相关文章

To top of page