“未找到匹配项”当使用匹配器的分组方法时

发布于 2024-11-01 03:35:07 字数 1093 浏览 2 评论 0原文

我使用 Pattern/Matcher 来获取 HTTP 响应中的响应代码。 groupCount 返回 1,但在尝试获取它时出现异常!知道为什么吗?

这是代码:

//get response code
String firstHeader = reader.readLine();
Pattern responseCodePattern = Pattern.compile("^HTTP/1\\.1 (\\d+) OK$");
System.out.println(firstHeader);
System.out.println(responseCodePattern.matcher(firstHeader).matches());
System.out.println(responseCodePattern.matcher(firstHeader).groupCount());
System.out.println(responseCodePattern.matcher(firstHeader).group(0));
System.out.println(responseCodePattern.matcher(firstHeader).group(1));
responseCode = Integer.parseInt(responseCodePattern.matcher(firstHeader).group(1));

这是输出:

HTTP/1.1 200 OK
true
1
Exception in thread "Thread-0" java.lang.IllegalStateException: No match found
 at java.util.regex.Matcher.group(Unknown Source)
 at cs236369.proxy.Response.<init>(Response.java:27)
 at cs236369.proxy.ProxyServer.start(ProxyServer.java:71)
 at tests.Hw3Tests$1.run(Hw3Tests.java:29)
 at java.lang.Thread.run(Unknown Source)

I'm using Pattern/Matcher to get the response code in an HTTP response. groupCount returns 1, but I get an exception when trying to get it! Any idea why?

Here's the code:

//get response code
String firstHeader = reader.readLine();
Pattern responseCodePattern = Pattern.compile("^HTTP/1\\.1 (\\d+) OK$");
System.out.println(firstHeader);
System.out.println(responseCodePattern.matcher(firstHeader).matches());
System.out.println(responseCodePattern.matcher(firstHeader).groupCount());
System.out.println(responseCodePattern.matcher(firstHeader).group(0));
System.out.println(responseCodePattern.matcher(firstHeader).group(1));
responseCode = Integer.parseInt(responseCodePattern.matcher(firstHeader).group(1));

And here's the output:

HTTP/1.1 200 OK
true
1
Exception in thread "Thread-0" java.lang.IllegalStateException: No match found
 at java.util.regex.Matcher.group(Unknown Source)
 at cs236369.proxy.Response.<init>(Response.java:27)
 at cs236369.proxy.ProxyServer.start(ProxyServer.java:71)
 at tests.Hw3Tests$1.run(Hw3Tests.java:29)
 at java.lang.Thread.run(Unknown Source)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寒尘 2024-11-08 03:35:07

pattern.matcher(input) 始终会创建一个新的匹配器,因此您需要再次调用 matches()

尝试:

Matcher m = responseCodePattern.matcher(firstHeader);
m.matches();
m.groupCount();
m.group(0); //must call matches() first
...

pattern.matcher(input) always creates a new matcher, so you'd need to call matches() again.

Try:

Matcher m = responseCodePattern.matcher(firstHeader);
m.matches();
m.groupCount();
m.group(0); //must call matches() first
...
站稳脚跟 2024-11-08 03:35:07

您不断地覆盖通过使用获得的匹配

System.out.println(responseCodePattern.matcher(firstHeader).matches());
System.out.println(responseCodePattern.matcher(firstHeader).groupCount());

每一行都会创建一个新的 Matcher 对象。

你应该去

Matcher matcher = responseCodePattern.matcher(firstHeader);
System.out.println(matcher.matches());
System.out.println(matcher.groupCount());

You are constantly overwriting the matches you got by using

System.out.println(responseCodePattern.matcher(firstHeader).matches());
System.out.println(responseCodePattern.matcher(firstHeader).groupCount());

Each line creates a new Matcher object.

You should go

Matcher matcher = responseCodePattern.matcher(firstHeader);
System.out.println(matcher.matches());
System.out.println(matcher.groupCount());
独守阴晴ぅ圆缺 2024-11-08 03:35:07

我也经历过同样的事情,但我写这个答案是因为我注意到了其他事情

正如其他人所说,你必须打电话

matcher.matches()

matcher.find();

在你可以打电话之前

matcher.group(1);

但是,如果您使用 results() 调用,则不必显式调用上面的这些方法结果立即可用

pattern
        .matcher(pathname)
        .results()
        .collect(Collectors.toList())
        .get(0)
        .group(1);

我认为这是故意的,但我只是在这里添加它

使用 matcher.results() 时无需调用 matcher.find() 或 matcher.matches()

I was experiencing the same, but I'm writing this answer because I noticed something else:

As others stated, you have to call either

matcher.matches()

or

matcher.find();

Before you can call

matcher.group(1);

However, if you are using the results() call, you won't explicitly have to call those methods above and the results are immediately available.

pattern
        .matcher(pathname)
        .results()
        .collect(Collectors.toList())
        .get(0)
        .group(1);

I think this is intentionally the case, but I'm just adding it here in addition

No need to call matcher.find() or matcher.matches() when using matcher.results().

蒲公英的约定 2024-11-08 03:35:07

或者您可以在 matcher.group(1) 之前调用 matcher.find();

Or you can call matcher.find(); before matcher.group(1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文