• <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 方法时

      quot;No match Foundquot; when using matcher#39;s group method(“未找到匹配项使用 matcher 的 group 方法时)

          <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 方法时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

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

                      <tbody id='tpJg0'></tbody>

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