Find a string in Html and get value after

Tags: , , , ,



I’m posting to a webpage and in my response I get a big chunk of HTML that will change next request. With groovy I’d like to find this string:

var WPQ1FormCtx = {"ListData":{"owshiddenversion":23,

The value “23” will change next time I post to the webpage, and I need that value.

With .contains I’ll find if string exists.

def htmlParse = Jsoup.parse(htmlResponse)
log.info a.contains('var WPQ1FormCtx = {"ListData":{"owshiddenversion":23,') 

But I need to write out the value after owshiddenversion in the string 'var WPQ1FormCtx = {"ListData":{"owshiddenversion":xxxxx, that can be anything from 1 to 100 000.

Answer

If I understand the string you are matching correctly, this will help you to do it in pure Groovy. You could wrap it in a method that is called as the test instead of .contains()

def stringPortion = 'var WPQ1FormCtx = {"ListData":{"owshiddenversion":23,'

def match = stringPortion =~ /"owshiddenversion":(d{1,6})/  //capture the match with ()

def matchPortion = match[0][1] //first capture in match

if (matchPortion) {
    println matchPortion
    def number = matchPortion.toInteger()
    if (number > 100000) {
        println 'number > 100000'
    }
    else if (number <= 10000) {
        println 'number <= 10000'
    }
}


Source: stackoverflow