gpt4 book ai didi

java - 当没有调用 'matching' 方法时,Matcher 抛出 IllegalStateException 的理由

转载 作者:行者123 更新时间:2023-12-03 18:05:54 25 4
gpt4 key购买 nike

TL;DR

Matcher 背后的设计决策是什么?的API?

背景

Matcher 有一种我没有预料到的行为,而且我找不到很好的理由。 API 文档说:

Once created, a matcher can be used to perform three different kinds of match operations: [...] Each of these methods returns a boolean indicating success or failure. More information about a successful match can be obtained by querying the state of the matcher.



API 文档进一步说的是:

The explicit state of a matcher is initially undefined; attempting to query any part of it before a successful match will cause an IllegalStateException to be thrown.



示例
String s = "foo=23,bar=42";
Pattern p = Pattern.compile("foo=(?<foo>[0-9]*),bar=(?<bar>[0-9]*)");
Matcher matcher = p.matcher(s);
System.out.println(matcher.group("foo")); // (1)
System.out.println(matcher.group("bar"));

此代码抛出一个
java.lang.IllegalStateException: No match found

(1) .为了解决这个问题,有必要调用 matches()或其他方法带来 Matcher进入允许 group() 的状态.以下作品:
String s = "foo=23,bar=42";
Pattern p = Pattern.compile("foo=(?<foo>[0-9]*),bar=(?<bar>[0-9]*)");
Matcher matcher = p.matcher(s);
matcher.matches(); // (2)
System.out.println(matcher.group("foo"));
System.out.println(matcher.group("bar"));

将调用添加到 matches() (2)设置 Matcher进入正确的状态来调用 group() .

问题,可能没有 build 性

为什么这个 API 是这样设计的?为什么不自动匹配 Matcher使用 Patter.matcher(String) 构建?

最佳答案

实际上,您误解了文档。第二次看你引用的声明: -

attempting to query any part of it before a successful match will cause an IllegalStateException to be thrown.



匹配器可能会抛出 IllegalStateException访问 matcher.group()如果没有找到匹配项。

因此,您需要使用以下测试来实际启动匹配过程:-
 - matcher.matches() //Or
- matcher.find()

下面的代码: -
Matcher matcher = pattern.matcher();  

只需创建一个 matcher实例。这实际上不会匹配字符串。哪怕是一场成功的比赛。
因此,您需要检查以下条件,以检查成功匹配:-
if (matcher.matches()) {
// Then use `matcher.group()`
}

如果 if 中的条件返回 false ,这意味着没有任何匹配。所以,如果你使用 matcher.group()不检查这个条件,你会得到 IllegalStateException如果没有找到匹配项。

假设,如果 Matcher是按照您所说的方式设计的,那么您将不得不执行 null检查是否找到匹配项,调用 matcher.group() , 像这样: -

你认为应该做的方式:-
// Suppose this returned the matched string
Matcher matcher = pattern.matcher(s);

// Need to check whether there was actually a match
if (matcher != null) { // Prints only the first match

System.out.println(matcher.group());
}

但是,如果你想打印任何进一步的匹配,因为一个模式可以在一个字符串中多次匹配,为此,应该有一种方法告诉匹配器找到下一个匹配。但是 null检查将无法做到这一点。为此,您必须将匹配器向前移动以匹配下一个字符串。因此, Matcher 中定义了各种方法。类服务于目的。 matcher.find()方法匹配字符串,直到找到所有匹配项。

还有其他方法, match字符串以不同的方式,这取决于你想要如何匹配。所以它最终在 Matcher上课做 matching对着弦。 Pattern类只创建一个 pattern匹配。如果 Pattern.matcher()前往 match模式,那么必须有某种方法来定义 match 的各种方法,如 matching可以有不同的方式。因此,需要 Matcher类(class)。

所以,它实际上是这样的: -
Matcher matcher = pattern.matcher(s);

// Finds all the matches until found by moving the `matcher` forward
while(matcher.find()) {
System.out.println(matcher.group());
}

因此,如果在字符串中找到 4 个匹配项,您的第一种方式将仅打印第一个,而第二种方式将通过移动 matcher 打印所有匹配项。前进以匹配下一个模式。

我希望这能说清楚。

Matcher 的文档类描述了它提供的三种方法的使用,其中说: -

A matcher is created from a pattern by invoking the pattern's matcher method. Once created, a matcher can be used to perform three different kinds of match operations:

  • The matches method attempts to match the entire input sequence against the pattern.

  • The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern.

  • The find method scans the input sequence looking for the next subsequence that matches the pattern.



不幸的是,我无法找到任何其他官方消息来源,明确说明此问题的原因和方式。

关于java - 当没有调用 'matching' 方法时,Matcher 抛出 IllegalStateException 的理由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39887636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com