“未找到匹配项”当使用匹配器的分组方法时
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
pattern.matcher(input)
始终会创建一个新的匹配器,因此您需要再次调用matches()
。尝试:
pattern.matcher(input)
always creates a new matcher, so you'd need to callmatches()
again.Try:
您不断地覆盖通过使用获得的匹配
每一行都会创建一个新的 Matcher 对象。
你应该去
You are constantly overwriting the matches you got by using
Each line creates a new Matcher object.
You should go
我也经历过同样的事情,但我写这个答案是因为我注意到了其他事情:
正如其他人所说,你必须打电话
或
在你可以打电话之前
但是,如果您使用 results() 调用,则不必显式调用上面的这些方法结果立即可用。
我认为这是故意的,但我只是在这里添加它
I was experiencing the same, but I'm writing this answer because I noticed something else:
As others stated, you have to call either
or
Before you can call
However, if you are using the results() call, you won't explicitly have to call those methods above and the results are immediately available.
I think this is intentionally the case, but I'm just adding it here in addition
或者您可以在
matcher.group(1)
之前调用matcher.find();
Or you can call
matcher.find();
beforematcher.group(1)