• <bdo id='f569i'></bdo><ul id='f569i'></ul>
    <i id='f569i'><tr id='f569i'><dt id='f569i'><q id='f569i'><span id='f569i'><b id='f569i'><form id='f569i'><ins id='f569i'></ins><ul id='f569i'></ul><sub id='f569i'></sub></form><legend id='f569i'></legend><bdo id='f569i'><pre id='f569i'><center id='f569i'></center></pre></bdo></b><th id='f569i'></th></span></q></dt></tr></i><div id='f569i'><tfoot id='f569i'></tfoot><dl id='f569i'><fieldset id='f569i'></fieldset></dl></div>

    <legend id='f569i'><style id='f569i'><dir id='f569i'><q id='f569i'></q></dir></style></legend>

    <tfoot id='f569i'></tfoot>

    1. <small id='f569i'></small><noframes id='f569i'>

      “未找到匹配项"使用 matcher 的 group 方法时

      时间:2023-10-13

          <tbody id='ZiUjS'></tbody>
        • <legend id='ZiUjS'><style id='ZiUjS'><dir id='ZiUjS'><q id='ZiUjS'></q></dir></style></legend>

          <tfoot id='ZiUjS'></tfoot>
        • <i id='ZiUjS'><tr id='ZiUjS'><dt id='ZiUjS'><q id='ZiUjS'><span id='ZiUjS'><b id='ZiUjS'><form id='ZiUjS'><ins id='ZiUjS'></ins><ul id='ZiUjS'></ul><sub id='ZiUjS'></sub></form><legend id='ZiUjS'></legend><bdo id='ZiUjS'><pre id='ZiUjS'><center id='ZiUjS'></center></pre></bdo></b><th id='ZiUjS'></th></span></q></dt></tr></i><div id='ZiUjS'><tfoot id='ZiUjS'></tfoot><dl id='ZiUjS'><fieldset id='ZiUjS'></fieldset></dl></div>
        • <small id='ZiUjS'></small><noframes id='ZiUjS'>

          • <bdo id='ZiUjS'></bdo><ul id='ZiUjS'></ul>

                本文介绍了“未找到匹配项"使用 matcher 的 group 方法时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                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?

                代码如下:

                //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)
                

                推荐答案

                pattern.matcher(input) 总是创建一个新的匹配器,所以你需要调用 matches() 再次.

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

                试试:

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

                这篇关于“未找到匹配项"使用 matcher 的 group 方法时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Azure Java 函数 -502-Bad Gateway 下一篇:如何向 OkHttp 请求拦截器添加标头?

                相关文章

                <small id='tpJg0'></small><noframes id='tpJg0'>

                <legend id='tpJg0'><style id='tpJg0'><dir id='tpJg0'><q id='tpJg0'></q></dir></style></legend>
                1. <tfoot id='tpJg0'></tfoot>

                    <i id='tpJg0'><tr id='tpJg0'><dt id='tpJg0'><q id='tpJg0'><span id='tpJg0'><b id='tpJg0'><form id='tpJg0'><ins id='tpJg0'></ins><ul id='tpJg0'></ul><sub id='tpJg0'></sub></form><legend id='tpJg0'></legend><bdo id='tpJg0'><pre id='tpJg0'><center id='tpJg0'></center></pre></bdo></b><th id='tpJg0'></th></span></q></dt></tr></i><div id='tpJg0'><tfoot id='tpJg0'></tfoot><dl id='tpJg0'><fieldset id='tpJg0'></fieldset></dl></div>
                      <bdo id='tpJg0'></bdo><ul id='tpJg0'></ul>