Need to get specific part of URL

Hello Team,

I need one help. I need to save one specific part of URL

This is the URL

https://this-is-my-url.com/24160553/checkouts/1209280661348825c36174f4f3bdce1b/thank_you

And want to extract

1209280661348825c36174f4f3bdce1b

and save it to url.

If the URL will always be the same size, then you can parse it using the “substring(startPosition, endPosition)” function, such as myUrl.substring(47, 79).

def myUrl = WebUI.getUrl()
def myContents = myUrl.substring(47, 79)
WebUI.comment("your contents are ${myContents}")

If the URL may not always be the same size, then you can try parsing it with the split("/") function, such as myUrl.split("/")[5]

def myUrl = WebUI.getUrl()
def myContents = myUrl.split("/")[5]
WebUI.comment("your contents are ${myContents}")

the 5 means we want the 6th item of the array that the split() creates (I think it’s the sixth we want) because the counting of arrays starts at zero.

2 Likes

Code

URL url = new URL("https://this-is-my-url.com/24160553/checkouts/1209280661348825c36174f4f3bdce1b/thank_you")
String path = url.getPath()   
println path                  //    /24160553/checkouts/1209280661348825c36174f4f3bdce1b/thank_you

List elements = path.split("/") as List
elements.eachWithIndex({ e, index ->
	println "${index} ${e}"
})
println "wanted: " + elements[3]

Output

0 
1 24160553
2 checkouts
3 1209280661348825c36174f4f3bdce1b
4 thank_you
wanted: 1209280661348825c36174f4f3bdce1b
1 Like