1

I have written this code in scala

val regex = "^(\\w+):\\/{2}(\\w*)\\.?([^\\/]*)([^\\?]*)\\??(.*)?".r
val x = "http://www.google.com/foo/bar/baz.html?x=10&y=20&z=20"
regex.findAllIn(x).matchData.foreach{x=> println(s"${x.group(1)} ${x.group(2)} ${x.group(3)} ${x.group(4)} ${x.group(5)}")

This works very well and I get the desired output of

http 
www 
google.com 
/foo/bar/baz.html 
x=10

However I need a way by which I can loop over the last part and generate strings for all parameters like

x=10
y=20
z=30

I am not clear as to how to extract the last part in a recurring way.

I know there are lots of existing regex to parse URLs on the internet ... but I am trying to understand regex and write my own. (just to learn).

1 Answer 1

3

If there is a fixed number of parameters, you can just repeat the last group as many times as necessary. For example, if there are always 3 parameters, you would use

"^(\\w+):\\/{2}(\\w+)\\.([^\\/]+)([^\\?]+)\\?([^&]+)&([^&]+)&([^&]+)".r

Otherwise, if the number of parameters varies, there is no regex-only solution for this problem, as it is not possible to extract multiple matches with a single match group. See this answer for more information.

To work around this problem, you can use the following regex

"^(\\w+):\\/{2}(\\w+)\\.([^\\/]+)([^\\?]+)\\?(.*)".r

in which the last group matches all parameters and then split the last match using something like

lastMatch.split("&")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.