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: " + nameMatcher.group(1));

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

Latest News

Get the latest freelance project announcements
Follow CodeGravity.com

Download ExtraWatch™

ExtraWatch™ FREE is Joomla, Wordpress, Prestashop, Magento plugin with following features: Live Stats, Nightly Email Reports, Goals, Graphs, Anti-spam, History and more