Java : Matcher (regex) 示例

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

注解 :

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

简介

通过解释模式对字符序列执行匹配操作的引擎。 (机器翻译)

Class diagram

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

// First match.
System.out.println(matcher.find()); // true
System.out.println(matcher.start()); // 0
System.out.println(matcher.end()); // 3
System.out.println(matcher.group()); // "abc"

// Next match.
System.out.println(matcher.find()); // true
System.out.println(matcher.start()); // 8
System.out.println(matcher.end()); // 13
System.out.println(matcher.group()); // "aaabc"

// No match.
System.out.println(matcher.find()); // false
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaabc");

final var stream = matcher.results();

stream.forEach(matchResult -> {

    System.out.println("------");
    System.out.println("start : " + matchResult.start());
    System.out.println("end : " + matchResult.end());
    System.out.println("group : " + matchResult.group());
});

// Result
// ↓
//------
//start : 0
//end : 3
//group : abc
//------
//start : 8
//end : 13
//group : aaabc

Methods

Matcher appendReplacement (StringBuffer sb, String replacement)

实现非终端附加和替换步骤。 (机器翻译)

final var pattern = Pattern.compile("cat");
final var matcher = pattern.matcher("one cat two cats in the yard");

final var sb = new StringBuffer();

while (matcher.find()) {
    matcher.appendReplacement(sb, "dog");
}

System.out.println(sb); // "one dog two dog"

matcher.appendTail(sb);

System.out.println(sb); // "one dog two dogs in the yard"

Matcher appendReplacement (StringBuilder sb, String replacement)

实现非终端附加和替换步骤。 (机器翻译)

final var pattern = Pattern.compile("cat");
final var matcher = pattern.matcher("one cat two cats in the yard");

final var sb = new StringBuilder();

while (matcher.find()) {
    matcher.appendReplacement(sb, "dog");
}

System.out.println(sb); // "one dog two dog"

matcher.appendTail(sb);

System.out.println(sb); // "one dog two dogs in the yard"

StringBuffer appendTail (StringBuffer sb)

实现终端附加和替换步骤。 (机器翻译)

final var pattern = Pattern.compile("cat");
final var matcher = pattern.matcher("one cat two cats in the yard");

final var sb = new StringBuffer();

while (matcher.find()) {
    matcher.appendReplacement(sb, "dog");
}

System.out.println(sb); // "one dog two dog"

matcher.appendTail(sb);

System.out.println(sb); // "one dog two dogs in the yard"

StringBuilder appendTail (StringBuilder sb)

实现终端附加和替换步骤。 (机器翻译)

final var pattern = Pattern.compile("cat");
final var matcher = pattern.matcher("one cat two cats in the yard");

final var sb = new StringBuilder();

while (matcher.find()) {
    matcher.appendReplacement(sb, "dog");
}

System.out.println(sb); // "one dog two dog"

matcher.appendTail(sb);

System.out.println(sb); // "one dog two dogs in the yard"

int end ()

返回最后一个匹配字符后的偏移量。 (机器翻译)

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

final var matcher = pattern.matcher(input);
System.out.println(matcher.matches()); // true

final var start = matcher.start();
System.out.println(start); // 0

final var end = matcher.end();
System.out.println(end); // 7

final var group = input.substring(start, end);
System.out.println(group); // "aaaaabc"
final var pattern = Pattern.compile("a+bc");
final var input = "012abcXYZ";

final var matcher = pattern.matcher(input);
System.out.println(matcher.find()); // true

final var start = matcher.start();
System.out.println(start); // 3

final var end = matcher.end();
System.out.println(end); // 6

final var group = input.substring(start, end);
System.out.println(group); // "abc"

int end (int group)

返回上一次匹配操作期间给定组捕获的子序列的最后一个字符后的偏移量。 (机器翻译)

final var pattern = Pattern.compile("(abc|efg)_012_(x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

System.out.println(matcher.start()); // 0
System.out.println(matcher.end()); // 11
System.out.println(matcher.group()); // "abc_012_xyz"

final var groupCount = matcher.groupCount();
System.out.println(groupCount); // 2

for (int i = 1; i <= groupCount; i++) {

    System.out.println("------");
    System.out.println("i : " + i);
    System.out.println("start : " + matcher.start(i));
    System.out.println("end : " + matcher.end(i));
    System.out.println("group : " + matcher.group(i));
}

// Result
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz

int end (String name)

返回上一个匹配操作期间由给定命名捕获组捕获的子序列的最后一个字符后的偏移量。 (机器翻译)

final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 11
System.out.println(result.group()); // "abc_012_xyz"

{
    final var name = "A";

    System.out.println(matcher.start(name)); // 0
    System.out.println(matcher.end(name)); // 3
    System.out.println(matcher.group(name)); // "abc"
}

{
    final var name = "B";

    System.out.println(matcher.start(name)); // 8
    System.out.println(matcher.end(name)); // 11
    System.out.println(matcher.group(name)); // "xyz"
}

boolean find ()

尝试找到输入序列中与模式匹配的下一个子序列。 (机器翻译)

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

System.out.println(matcher.hasMatch()); // false

{
    System.out.println(matcher.find()); // true
    System.out.println(matcher.hasMatch()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 0
    System.out.println(result.end()); // 3
    System.out.println(result.group()); // "abc"
}

{
    System.out.println(matcher.find()); // true
    System.out.println(matcher.hasMatch()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 8
    System.out.println(result.end()); // 13
    System.out.println(result.group()); // "aaabc"
}

System.out.println(matcher.find()); // false
System.out.println(matcher.hasMatch()); // false

boolean find (int start)

重置此匹配器,然后尝试从指定的索引开始找到与模式匹配的输入序列的下一个子序列。 (机器翻译)

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("012 abc xyz aaabc");

{
    System.out.println(matcher.find(8)); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 12
    System.out.println(result.end()); // 17
    System.out.println(result.group()); // "aaabc"
}

{
    System.out.println(matcher.find(1)); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 4
    System.out.println(result.end()); // 7
    System.out.println(result.group()); // "abc"
}

String group ()

返回与上一个匹配项匹配的输入子序列。 (机器翻译)

final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaabc xyz");

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aaabc"

System.out.println(matcher.find()); // false

String group (int group)

返回上一次匹配操作期间给定组捕获的输入子序列。 (机器翻译)

final var pattern = Pattern.compile("(abc|efg)_012_(x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

System.out.println(matcher.start()); // 0
System.out.println(matcher.end()); // 11
System.out.println(matcher.group()); // "abc_012_xyz"

final var groupCount = matcher.groupCount();
System.out.println(groupCount); // 2

for (int i = 1; i <= groupCount; i++) {

    System.out.println("------");
    System.out.println("i : " + i);
    System.out.println("start : " + matcher.start(i));
    System.out.println("end : " + matcher.end(i));
    System.out.println("group : " + matcher.group(i));
}

// Result
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz

String group (String name)

返回上一个匹配操作期间由给定命名捕获组捕获的输入子序列。 (机器翻译)

final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 11
System.out.println(result.group()); // "abc_012_xyz"

{
    final var name = "A";

    System.out.println(matcher.start(name)); // 0
    System.out.println(matcher.end(name)); // 3
    System.out.println(matcher.group(name)); // "abc"
}

{
    final var name = "B";

    System.out.println(matcher.start(name)); // 8
    System.out.println(matcher.end(name)); // 11
    System.out.println(matcher.group(name)); // "xyz"
}

int groupCount ()

返回此匹配器模式中的捕获组的数量。 (机器翻译)

final var pattern = Pattern.compile("(abc|efg)_012_(x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

System.out.println(matcher.start()); // 0
System.out.println(matcher.end()); // 11
System.out.println(matcher.group()); // "abc_012_xyz"

final var groupCount = matcher.groupCount();
System.out.println(groupCount); // 2

for (int i = 1; i <= groupCount; i++) {

    System.out.println("------");
    System.out.println("i : " + i);
    System.out.println("start : " + matcher.start(i));
    System.out.println("end : " + matcher.end(i));
    System.out.println("group : " + matcher.group(i));
}

// Result
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz

boolean hasAnchoringBounds ()

查询此匹配器的区域边界的锚定。 (机器翻译)

final var pattern = Pattern.compile("^abc xyz$");
final var input = "012 abc xyz";

{
    final var matcher = pattern.matcher(input);

    System.out.println(matcher.hasAnchoringBounds()); // true

    // Sets the limits of this matcher's region.
    System.out.println(input.substring(4, 11)); // "abc xyz"
    matcher.region(4, 11);

    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 4
    System.out.println(result.end()); // 11
    System.out.println(result.group()); // "abc xyz"
}

{
    final var matcher = pattern.matcher(input);

    matcher.useAnchoringBounds(false);
    System.out.println(matcher.hasAnchoringBounds()); // false

    matcher.region(4, 11);
    System.out.println(matcher.find()); // false
}

boolean hasMatch ()

返回这是否包含来自前一个匹配或查找操作的有效匹配。 (机器翻译)

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

System.out.println(matcher.hasMatch()); // false

{
    System.out.println(matcher.find()); // true
    System.out.println(matcher.hasMatch()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 0
    System.out.println(result.end()); // 3
    System.out.println(result.group()); // "abc"
}

{
    System.out.println(matcher.find()); // true
    System.out.println(matcher.hasMatch()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 8
    System.out.println(result.end()); // 13
    System.out.println(result.group()); // "aaabc"
}

System.out.println(matcher.find()); // false
System.out.println(matcher.hasMatch()); // false

boolean hasTransparentBounds ()

查询此匹配器的区域边界的透明度。 (机器翻译)

final var pattern = Pattern.compile("\\by...");
final var input = "xyz12 yz34";

{
    final var matcher = pattern.matcher(input);

    System.out.println(matcher.hasTransparentBounds()); // false

    // Sets the limits of this matcher's region.
    System.out.println(input.substring(1, 10)); // "yz12 yz34"
    matcher.region(1, 10);

    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.group()); // "yz12"
}

{
    final var matcher = pattern.matcher(input);

    matcher.useTransparentBounds(true);
    System.out.println(matcher.hasTransparentBounds()); // true

    matcher.region(1, 10);

    // This matcher to use transparent bounds.
    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.group()); // "yz34"
}

boolean hitEnd ()

如果在该匹配器执行的最后一个匹配操作中搜索引擎命中了输入的结尾,则返回 true。 (机器翻译)

final var pattern = Pattern.compile("cat");
final var matcher = pattern.matcher("one cat two cats in the yard");

while (matcher.find()) {

    System.out.println("-------");
    System.out.println("hitEnd : " + matcher.hitEnd());

    final var result = matcher.toMatchResult();
    System.out.println("start : " + result.start());
    System.out.println("end : " + result.end());
}

// Result
// ↓
//-------
//hitEnd : false
//start : 4
//end : 7
//-------
//hitEnd : false
//start : 12
//end : 15

System.out.println(matcher.hitEnd()); // true

boolean lookingAt ()

尝试从区域开头开始将输入序列与模式进行匹配。 (机器翻译)

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

System.out.println(matcher.lookingAt()); // true

final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 5
System.out.println(result.group()); // "aaabc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abcXYZ");

System.out.println(matcher.lookingAt()); // true

final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 3
System.out.println(result.group()); // "abc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("012abc");

System.out.println(matcher.lookingAt()); // false

boolean matches ()

尝试将整个区域与模式进行匹配。 (机器翻译)

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

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 3
System.out.println(result.group()); // "abc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("aaaaabc");

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 7
System.out.println(result.group()); // "aaaaabc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("012abc");

System.out.println(matcher.matches()); // false
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abcZ");

System.out.println(matcher.matches()); // false

Map<String,Integer> namedGroups ()

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

final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

System.out.println("-- groups --");
for (final var entry : matcher.namedGroups().entrySet()) {
    System.out.println("entry : " + entry);

    final var num = entry.getValue();
    System.out.println("  group : " + matcher.group(num));
}

// Result
// ↓
//true
//-- groups --
//entry : A=1
//  group : abc
//entry : B=2
//  group : xyz

Pattern pattern ()

返回该匹配器解释的模式。 (机器翻译)

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

System.out.println(matcher.pattern()); // "a+bc"

static String quoteReplacement (String s)

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

final var pattern = Pattern.compile(" ");
final var matcher = pattern.matcher("abc 012 xyz");

final var replacement = Matcher.quoteReplacement("$");
System.out.println(replacement); // "\$"

final var ret = matcher.replaceAll(replacement);
System.out.println(ret); // "abc$012$xyz"

Matcher region (int start, int end)

设置此匹配器区域的限制。 (机器翻译)

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

final var input = "aaabccc";
final var matcher = pattern.matcher(input);

System.out.println(input.substring(2, 6)); // "abcc"
matcher.region(2, 6);

System.out.println(matcher.regionStart()); // 2
System.out.println(matcher.regionEnd()); // 6

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.group()); // "abcc"
// An example without the region method.
final var pattern = Pattern.compile("a+bc+");
final var matcher = pattern.matcher("aaabccc");

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.group()); // "aaabccc"

int regionEnd ()

报告此匹配器区域的结束索引(不包括)。 (机器翻译)

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

final var input = "aaabccc";
final var matcher = pattern.matcher(input);

System.out.println(input.substring(2, 6)); // "abcc"
matcher.region(2, 6);

System.out.println(matcher.regionStart()); // 2
System.out.println(matcher.regionEnd()); // 6

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.group()); // "abcc"
// An example without the region method.
final var pattern = Pattern.compile("a+bc+");
final var matcher = pattern.matcher("aaabccc");

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.group()); // "aaabccc"

int regionStart ()

报告此匹配器区域的起始索引。 (机器翻译)

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

final var input = "aaabccc";
final var matcher = pattern.matcher(input);

System.out.println(input.substring(2, 6)); // "abcc"
matcher.region(2, 6);

System.out.println(matcher.regionStart()); // 2
System.out.println(matcher.regionEnd()); // 6

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.group()); // "abcc"
// An example without the region method.
final var pattern = Pattern.compile("a+bc+");
final var matcher = pattern.matcher("aaabccc");

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.group()); // "aaabccc"

String replaceAll (String replacement)

用给定的替换字符串替换与模式匹配的输入序列的每个子序列。 (机器翻译)

final var pattern = Pattern.compile("a*b");
final var matcher = pattern.matcher("aabfooaabfooabfoob");

final var result = matcher.replaceAll("-");
System.out.println(result); // "-foo-foo-foo-"

String replaceAll (Function<MatchResult,String> replacer)

用将给定的替换函数应用于与该子序列相对应的此匹配器的匹配结果的结果来替换与模式匹配的输入序列的每个子序列。 (机器翻译)

final var pattern = Pattern.compile("dog");
final var matcher = pattern.matcher("zzzdogzzzdogzzz");

final var result = matcher.replaceAll(mr -> mr.group().toUpperCase());
System.out.println(result); // "zzzDOGzzzDOGzzz"

String replaceFirst (String replacement)

用给定的替换字符串替换与模式匹配的输入序列的第一个子序列。 (机器翻译)

final var pattern = Pattern.compile("dog");
final var matcher = pattern.matcher("zzzdogzzzdogzzz");

final var result = matcher.replaceFirst("cat");
System.out.println(result); // "zzzcatzzzdogzzz"

String replaceFirst (Function<MatchResult,String> replacer)

用将给定的替换函数应用于与该子序列对应的此匹配器的匹配结果的结果来替换与模式匹配的输入序列的第一个子序列。 (机器翻译)

final var pattern = Pattern.compile("dog");
final var matcher = pattern.matcher("zzzdogzzzdogzzz");

final var result = matcher.replaceFirst(mr -> mr.group().toUpperCase());
System.out.println(result); // "zzzDOGzzzdogzzz"

boolean requireEnd ()

如果更多输入可以将正匹配变为负匹配,则返回 true。 (机器翻译)

final var pattern = Pattern.compile("abc$");
final var matcher = pattern.matcher("abc");

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"
System.out.println(matcher.requireEnd()); // true
final var pattern = Pattern.compile("abc");
final var matcher = pattern.matcher("abcd");

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"
System.out.println(matcher.requireEnd()); // false

Matcher reset ()

重置此匹配器。 (机器翻译)

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

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aabc"

System.out.println(matcher.find()); // false

matcher.reset();

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aabc"

System.out.println(matcher.find()); // false

Matcher reset (CharSequence input)

使用新的输入序列重置此匹配器。 (机器翻译)

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

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aabc"

System.out.println(matcher.find()); // false

matcher.reset("aaaabc");

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aaaabc"

System.out.println(matcher.find()); // false

Stream<MatchResult> results ()

返回与模式匹配的输入序列的每个子序列的匹配结果流。 (机器翻译)

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

final var stream = matcher.results();

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

// Result
// ↓
// "abc"
// "aabc"

int start ()

返回上一个匹配的起始索引。 (机器翻译)

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

final var matcher = pattern.matcher(input);
System.out.println(matcher.matches()); // true

final var start = matcher.start();
System.out.println(start); // 0

final var end = matcher.end();
System.out.println(end); // 7

final var group = input.substring(start, end);
System.out.println(group); // "aaaaabc"
final var pattern = Pattern.compile("a+bc");
final var input = "012abcXYZ";

final var matcher = pattern.matcher(input);
System.out.println(matcher.find()); // true

final var start = matcher.start();
System.out.println(start); // 3

final var end = matcher.end();
System.out.println(end); // 6

final var group = input.substring(start, end);
System.out.println(group); // "abc"

int start (int group)

返回上一个匹配操作期间给定组捕获的子序列的起始索引。 (机器翻译)

final var pattern = Pattern.compile("(abc|efg)_012_(x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

System.out.println(matcher.start()); // 0
System.out.println(matcher.end()); // 11
System.out.println(matcher.group()); // "abc_012_xyz"

final var groupCount = matcher.groupCount();
System.out.println(groupCount); // 2

for (int i = 1; i <= groupCount; i++) {

    System.out.println("------");
    System.out.println("i : " + i);
    System.out.println("start : " + matcher.start(i));
    System.out.println("end : " + matcher.end(i));
    System.out.println("group : " + matcher.group(i));
}

// Result
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz

int start (String name)

返回上一个匹配操作期间由给定命名捕获组捕获的子序列的起始索引。 (机器翻译)

final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
final var matcher = pattern.matcher("abc_012_xyz");

System.out.println(matcher.matches()); // true

final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 11
System.out.println(result.group()); // "abc_012_xyz"

{
    final var name = "A";

    System.out.println(matcher.start(name)); // 0
    System.out.println(matcher.end(name)); // 3
    System.out.println(matcher.group(name)); // "abc"
}

{
    final var name = "B";

    System.out.println(matcher.start(name)); // 8
    System.out.println(matcher.end(name)); // 11
    System.out.println(matcher.group(name)); // "xyz"
}

MatchResult toMatchResult ()

将此匹配器的匹配状态作为 MatchResult 返回。 (机器翻译)

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

{
    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 0
    System.out.println(result.end()); // 3
    System.out.println(result.group()); // "abc"
}

{
    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 4
    System.out.println(result.end()); // 8
    System.out.println(result.group()); // "aabc"
}

String toString ()

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

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

System.out.println(matcher.find()); // true

final var str = matcher.toString();

// java.util.regex.Matcher[pattern=a+bc region=0,8 lastmatch=abc]
System.out.println(str);

Matcher useAnchoringBounds (boolean b)

设置此匹配器的区域边界的锚定。 (机器翻译)

final var pattern = Pattern.compile("^abc xyz$");
final var input = "012 abc xyz";

{
    final var matcher = pattern.matcher(input);

    System.out.println(matcher.hasAnchoringBounds()); // true

    // Sets the limits of this matcher's region.
    System.out.println(input.substring(4, 11)); // "abc xyz"
    matcher.region(4, 11);

    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.start()); // 4
    System.out.println(result.end()); // 11
    System.out.println(result.group()); // "abc xyz"
}

{
    final var matcher = pattern.matcher(input);

    matcher.useAnchoringBounds(false);
    System.out.println(matcher.hasAnchoringBounds()); // false

    matcher.region(4, 11);
    System.out.println(matcher.find()); // false
}

Matcher usePattern (Pattern newPattern)

更改此匹配器用来查找匹配的模式。 (机器翻译)

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

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aabc"

System.out.println(matcher.find()); // false

matcher.usePattern(Pattern.compile("x.."));

System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "xyz"

Matcher useTransparentBounds (boolean b)

设置此匹配器区域边界的透明度。 (机器翻译)

final var pattern = Pattern.compile("\\by...");
final var input = "xyz12 yz34";

{
    final var matcher = pattern.matcher(input);

    System.out.println(matcher.hasTransparentBounds()); // false

    // Sets the limits of this matcher's region.
    System.out.println(input.substring(1, 10)); // "yz12 yz34"
    matcher.region(1, 10);

    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.group()); // "yz12"
}

{
    final var matcher = pattern.matcher(input);

    matcher.useTransparentBounds(true);
    System.out.println(matcher.hasTransparentBounds()); // true

    matcher.region(1, 10);

    // This matcher to use transparent bounds.
    System.out.println(matcher.find()); // true

    final var result = matcher.toMatchResult();
    System.out.println(result.group()); // "yz34"
}

相关文章

To top of page