0

I trying to use java pattern matching to extract the http response code from a single line in the http response header, but for some reason it is not matching. I dont suppose anyone could point out where i am going wrong..?

This is my code:`

private static String getSubString(String regex, String data ) {
          Pattern pattern = Pattern.compile(regex);

          Matcher matcher = pattern.matcher(data);
          if(matcher.matches()) {
              return matcher.group(1);
          }
          return null;
      }`

And this is my test input to the method:

data = "HTTP/1.1 400 Bad Request";
regex = "([0-9][0-9][0-9])";

for some reason matcher.matches() is returning false.

Thanks heaps for any help anyone can provide Corey

Fishingfon
  • 1,034
  • 3
  • 19
  • 33
  • Just a question; Wouldnt it be better to use the actual response object, if available? Then you would not have to do such parsing/patterng matching. – vegaasen Mar 23 '18 at 10:29
  • You need to use `matcher.find()`. `matches` is for matching the whole string.Which in your case it is false. – Arun Gowda Mar 23 '18 at 10:35

0 Answers0