“No match Found” when using matcher's group method

后端 未结 2 1828
终归单人心
终归单人心 2020-11-27 18:09

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 g

2条回答
  •  星月不相逢
    2020-11-27 18:29

    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());
    

提交回复
热议问题