Reputation: 832
I am trying to come up with a regular expression that matches the following URLs:
http://test.com/bobs/your/uncle/1
http://test.com/bobs/your/uncle/5
http://test.com/bobs/your/uncle/5?tab=1
But does not match the following URLs:
http://test.com/bobs/your/uncle/1/brah/5
http://test.com/bobs/your/uncle/1/brah/5?tab=2
I tried the following regex
/bobs/your/uncle/\d+\??
Which works for the first three URLs above, but not the last two.
Upvotes: 1
Views: 4856
Reputation: 107297
You need to use start and end anchors :
'bobs/your/uncle/\d+\??[^/]*$'
See demo https://regex101.com/r/pP2fL1/1
you can use [^/]*
to match some extra strings like tab=1
which match any thing except /
.
Upvotes: 3
Reputation: 785276
You can use this regex:
/bobs/your/uncle/\d+(?:\?|$)
(?:\?|$)
will match ?
or end of input after your desired input URIs.
Upvotes: 4