Hi folks, I want to extract from an url the subst...
# android
l
Hi folks, I want to extract from an url the substring before
v[0..9]
e.g:
Copy code
<https://baseurl.test/v1/example/{id}>
the result should be only
<https://baseurl.test/>
Where the version number can be 1,2....9 Is there a way to do this?
t
There is String.substringBefore, but it takes a specific String. For this I think you need to do:
Copy code
Regex("v[0-9]").split(url).firstOrNull()
Also, this would be better asked in #stdlib
v
But splitting is a bit wasty if you use Regex anyway. Better do it efficiently with a lookahead:
Copy code
Regex("^.*?/(?=v[0-9]/)").find(url)?.value
l
Thank you guys, the regex is what I missed 🙂