0

I'm trying to extract a form element value using regexp:

Pattern pattern = Pattern.compile("name=\"(token)\"[^>]*value=\"([^\"]+)\"", 2);

Matcher matcher = pattern.matcher(result); 

if(matcher.find())
{
    String value = matcher.group(2);
}
<input type="hidden" name="token" value="YToxOntzOjU"/>

However, my matcher yields no results. What am I missing?

Johan
  • 34,157
  • 52
  • 174
  • 280

1 Answers1

1

You should not parse HTML using regular expressions, but your written code seems to work fine?

String result  = "<input type=\"hidden\" name=\"token\" value=\"YToxOntzOjU\"/>";

Pattern pattern = Pattern.compile("name=\"(token)\"[^>]*value=\"([^\"]+)\"", 2);
Matcher matcher = pattern.matcher(result); 

if (matcher.find()) {
   String value = matcher.group(2);
   System.out.println(value); //=> "YToxOntzOjU"
}

Working Demo

Community
  • 1
  • 1
hwnd
  • 67,942
  • 4
  • 86
  • 123