1

While fully aware this is likely a duplicate, I simply cannot find a solution to this problem.

How to get a parameter anywhere it exist in a URL?

Consider these urls:
https://any.domain.com/uri-slug/random-string/specific-slug/?version='2'
https://any.domain.com/uri-slug/random-string/specific-slug/?paramOne=1&version=2
https://any.domain.com/uri-slug/random-string/specific-slug/?paramOne=1&paramTwo=2&version=2

I want to get version=2 out of these URLs.

This Regex will return true if version=2 starts the parameters, but not if it's after the first param.

https:\/\/(\w)+.domain.com\/(\w)+\/(\S)+\/specific-slug\/[?&]+(version=2)

How can I get version=2 to return true if it's present anywhere in the parameters?

5
  • If the version no is fixed i.e 2. A literal regex /version=2/ will return it if present in URL. Commented Mar 10, 2016 at 16:10
  • 2
    And why so complicated? version='?\d+ will do the same. Additionally, which language do you use? If it is always version=2, you do not need a regex at all, so what is your question, really? Commented Mar 10, 2016 at 16:10
  • @anubhava: OP said anywhere in the parameters, so I wonder if there's really a structure but we'll see. Commented Mar 10, 2016 at 16:17
  • Yes, I have to verify the entire domain up to that specific-slug (also where the second \w is will have to be verified as well). After specific slug I need to know when version=2 is present and when it isn't. This is for tracking via hotjar. Commented Mar 10, 2016 at 16:23
  • Also, yes there is a structure. Sometimes version=2 will be the first param, other times it will be the last out of possibly four. Commented Mar 10, 2016 at 16:23

2 Answers 2

3

I think you need to allow for any character to appear 0 or more times (.*?) before looking for the [?&] one or more times. You also might need to account for the single quote characters. In your first string, which I did not include below. The portion of the regex (\w)+\/(\S)+ also will not match your "uri-slug" because it contains a dash (non-word) character.

https:\/\/(\w)+.domain.com\/(\w)+\/(\S)+\/specific-slug\/.*?[?&]+(version=2)

I'd suggest using a regex tester tool to get it exactly the way you want it. Something like https://regex101.com/ could work but there are many.

Sign up to request clarification or add additional context in comments.

5 Comments

Ha, I've been on regex101 all day. I've tried everything but that .*? and that solved it. Thank you!
If you don't mind, is there a character that would mean not present? In other words, return true if version=2 isn't present?
It may not be super specific but the carat character in brackets means "anything but these characters". [^abc] means anything but an a, b or c. version=2 not being present might mean you just don't get a regex match (since you're checking specifically for it)
Yeah i've tried https:\/\/(\w)+.domain.com\/(\w)+\/(\S)+\/specific-slug\/.*?[?&]+([^version=2]) but it returns false both with and without the version parameter. Thanks so much for your help, regex is clearly a weak point for me.
With what you are trying to do, you should check this post:stackoverflow.com/questions/1240275/…
0

Check if the String contains "Version=2" by simply using String.contains() method in java if you are using it as language for your code.

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.