Reputation: 2494
I have the below regex, but how can I remove the querystring entirely if it is present:
^~/(.*)/restaurant/(.*)
eg. the url
/seattle/restaurant/sushi?page=2
or
/seattle/restaurant/sushi?somethingelse=something
or
/seatthe/restaurant/sushi
should just return seattle
and restaurant
and sushi
and remove any querystring if it is present.
(sorry for reposting a similar question, but I couldn't get the answer to work in my previous question).
thanks
Thomas
Upvotes: 3
Views: 15208
Reputation: 52185
This regex:
(/[^?]+).*
Should match the initial section of your URL and put it in a group.
So it will match /seattle/restaurant/sushi
and put the value in a group.
You can use something like this: (/.*?/restaurant[^?]+).*
if you want to handle just URLs with the word restaurant
as the second word between the slashes.
Edit: Something like so should yield 3 groups: /(.*?)/(restaurant)/([^?]+).*
. Group 1 being seatthe
, group 2 being restaurant
and group 3 being sushi
. If after the last /
there is a ?
, the regex discards the ?
and everything which follows.
Upvotes: 8
Reputation: 126742
You should change your final /./
to match "anything but a question mark" like this
^~/(.*)/restaurant/([^?]*)
Upvotes: 1