Check our new

Extracting a value with Regular expressions in Java

I wanted to extract an exact value from the given String, in this situation it was a value of the VIEWSTATE input element: In java there are many ways how to work with regular expressions, but it returned me only that the string matched the exact pattern, but not the extracted part of the string I needed. Here's the code that does all the magic:

Pattern pattern = Pattern.compile("name=\"__VIEWSTATE\" value=\"(.*?)\"", Pattern.CASE_INSENSITIVE);
// finds a viewstate value in the string with regular expression

Matcher matcher = pattern.matcher(response);
while (matcher.find()) {
System.out.println("Found a match: " +;

The group(1) element returns the value enclosed by the round brackets. Then you can work with the value itself. Enjoy

Add comment

Security code

This website uses cookies to ensure you get the best experience on our website. Learn More.

Got It!