Help with Regex in URL, for dynamic data

I have the following input:

String input = “http://somewebsite.com/path?nonce=17&cmac=2e1224422baa44134e87cdff2303d417

How do I need to write the strings nonceRegex and cmacRegex to validate the input against teh below target:

String target = WebUI.concatenate(“http://somewebsite.com/path?nonce=”, nonceRegex, “&cmac=”, cmacRegex)

with

WebUI.verifyMatch (input, target, true)

nonceRegex = “([0-9])*\d”
cmacRegex = “[a-f0-9]{16}”
don’t help …

Hi Erik,

given that nonce is always first parameter, this code would work.

String input = "http://somewebsite.com/path?nonce=17&cmac=2e1224422baa44134e87cdff2303d417"

String nonce = input.substring(input.indexOf("nonce=") + 6, input.indexOf("&"))
String cmac = input.substring(input.indexOf("cmac=") + 5)

assert nonce.matches("([0-9])*\\d")
assert cmac.matches("[a-f0-9]{32}")

Btw. UUID contains 32 characters, not 16. :slight_smile:

1 Like

My code is here

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

Map<String,String> queryParameters(String urlString) {
	URL url = new URL(urlString)
	// get all query params as list
	def queryParams = url.query?.split('&') // safe operator for urls without query params
	// transform the params list to a Map spliting
	// each query param
    if (queryParams != null) {
	    return queryParams.collectEntries { param -> param.split('=').collect { URLDecoder.decode(it,'UTF-8') }}
    } else {
        return [:]
    }
}

String queryParameter(String urlString, String name) {
	def mapParams = queryParameters(urlString)
	return mapParams[name]
}

String input = 'http://somewebsite.com/path?nonce=17&cmac=2e1224422baa44134e87cdff2303d417'
String nonce = queryParameter(input,'nonce')
String cmac  = queryParameter(input,'cmac')

println "queryParameters is ${queryParameters(input)}"
println "nonce is ${nonce}"
println "cmac is ${cmac}"

WebUI.verifyMatch(nonce, '[0-9]*\\d', true)
WebUI.verifyMatch(cmac , '[a-f0-9]{32}', true)

2 Likes

? character has special meaning as a Regular expression construct. You are not careful enough for it, therefore your trial to match will fail.

Thanks for the hint here … I am leaning towards kazurayam’s solution below … but this is helping already.