Hi,
I need to filter out the last part of the url I get back from the `WebUI.getURL()` function.
I have the Regex to filter but I do not know how to do it. I will need to store the value to use it later in the script.
Here is my Regex
(?:(\\w+)\\/$|(\\w+)\\?|(\\w+)$)
Thanks
Hi Laszlo,
you don’t need regex if you want to get a string after last slash in your URL. You can do it this way:
// replace url string by WebUI.getURL()String url = 'http://www.katalon.com/firstpart/lastpart'// this method gets index of last "/"
int lastSlashIndex = url.lastIndexOf("/")// get a substring after last "/" to the end
String lastUrlPart = url.substring(lastSlashIndex + 1)
Thank you Marek,
The problem is that if I need test a webpae that has special character like ?
or in my case there is a trailing `slash` and I need the word between the last two slashes. Ie.: www.yourwebsite.com/subpage/
So I need the word `subpage` from the URL
Indeed. Then, you must use Pattern and Matcher classes from java.util.regex package.
import java.util.regex.Matcher
import java.util.regex.Pattern
import com.kms.katalon.core.util.KeywordUtil
String url = 'www.yourwebsite.com/subpage/'
Pattern regexPat = Pattern.compile('(?:(\\w+)\\/$|(\\w+)\\?|(\\w+)$)')
Matcher mat = regexPat.matcher(url)
if(mat.find()) {
String result = mat.group()
println result
}
else {
KeywordUtil.markFailed('Substring not found.')
}// pattern found:subpage/
You may need to improve your regex string to remove trailing slash from the result substring.
2 Likes
Thank you, works perfectly.
I really had to upda the regex string as it stopped at the -
and did not take all words between the slashes.
(?:(\[^\\\/\]+)\\\/$|(\[^\\\/\]+)\\\?|(\[^\\\/\]+)$)