Java : MatchResult (regex) 示例

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

注解 :

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

简介

匹配操作的结果。 (机器翻译)

Class diagram

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

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 : 14
//group : aaaabc

Methods

int end ()

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

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

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

final var matchResult = matcher.toMatchResult();

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

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 : 14
//group : aaaabc

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

final var matchResult = matcher.toMatchResult();

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

final var groupCount = matchResult.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 : " + matchResult.start(i));
    System.out.println("end : " + matchResult.end(i));
    System.out.println("group : " + matchResult.group(i));
}

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

default 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 matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"

{
    final var name = "A";

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

{
    final var name = "B";

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

String group ()

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

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

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

final var matchResult = matcher.toMatchResult();

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

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 : 14
//group : aaaabc

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

final var matchResult = matcher.toMatchResult();

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

final var groupCount = matchResult.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 : " + matchResult.start(i));
    System.out.println("end : " + matchResult.end(i));
    System.out.println("group : " + matchResult.group(i));
}

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

default 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 matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"

{
    final var name = "A";

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

{
    final var name = "B";

    System.out.println(matchResult.start(name)); // 8
    System.out.println(matchResult.end(name)); // 11
    System.out.println(matchResult.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

final var matchResult = matcher.toMatchResult();

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

final var groupCount = matchResult.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 : " + matchResult.start(i));
    System.out.println("end : " + matchResult.end(i));
    System.out.println("group : " + matchResult.group(i));
}

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

default boolean hasMatch ()

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

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

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

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

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

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

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

    final var matchResult = matcher.toMatchResult();
    System.out.println(matchResult.hasMatch()); // false
}

default 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
final var matchResult = matcher.toMatchResult();

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

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

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

int start ()

返回比赛的起始索引。 (机器翻译)

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

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

final var matchResult = matcher.toMatchResult();

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

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 : 14
//group : aaaabc

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

final var matchResult = matcher.toMatchResult();

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

final var groupCount = matchResult.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 : " + matchResult.start(i));
    System.out.println("end : " + matchResult.end(i));
    System.out.println("group : " + matchResult.group(i));
}

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

default 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 matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"

{
    final var name = "A";

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

{
    final var name = "B";

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

相关文章

To top of page