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

后端 未结 2 1805
终归单人心
终归单人心 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:19

    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
    ...
    
    0 讨论(0)
  • 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());
    
    0 讨论(0)
提交回复
热议问题