Search code examples
regexscalagatling

Problems with extracting json respnse value in scala using brackets


Have tried using " and ' in different combinations to extract the value 13029416243 from the JSON response body I get in a gatling/scala script

,\"initialString\":\"13029416243\"},

This has been some of my try outs:

 .check(regex("initialString(.*?)}").exists.saveAs("initialString"))

and

 .check(regex("initialString\\\":\\\"(.*?)\\\"}").exists.saveAs("initialString"))

where the last one results in this output in the log:

---- Errors --------------------------------------------------------------------

regex(initialString\":\"(.*?)\"}).find.exists, found nothing

Any help on how to obtain the value?


Solution

  • You can make the use of lookbehinds. But to be honest. Json should be parsed with json parsers. Regex is not always a reliable tool for searching in jsons.

    (?<=initialString\\":\\")\d+\b
    

    https://regex101.com/r/Wgeurd/1/