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?

thatgibbyguy
  • 4,033
  • 3
  • 23
  • 39
  • If the version no is fixed i.e 2. A literal regex `/version=2/` will return it if present in URL. –  Mar 10 '16 at 16:10
  • 2
    And why so complicated? [`version='?\d+`](https://regex101.com/r/bK9dX2/1) 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? – Jan Mar 10 '16 at 16:10
  • @anubhava: OP said *anywhere* in the parameters, so I wonder if there's really a structure but we'll see. – Jan Mar 10 '16 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. – thatgibbyguy Mar 10 '16 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. – thatgibbyguy Mar 10 '16 at 16:23

2 Answers2

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.

NickT
  • 214
  • 1
  • 5
  • Ha, I've been on regex101 all day. I've tried everything but that `.*?` and that solved it. Thank you! – thatgibbyguy Mar 10 '16 at 16:26
  • 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? – thatgibbyguy Mar 10 '16 at 17:08
  • 1
    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) – NickT Mar 10 '16 at 17:27
  • 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. – thatgibbyguy Mar 10 '16 at 18:01
  • 1
    With what you are trying to do, you should check this post:http://stackoverflow.com/questions/1240275/how-to-negate-specific-word-in-regex – NickT Mar 10 '16 at 20:17
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.

Rishal
  • 1,480
  • 1
  • 11
  • 19