3

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

2 Answers 2

8

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.

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

5 Comments

I would like to match "seattle" and "sushi" as separate matches. Also if "sushi" is proceeded by a querystring. In which case I would like to match "sushi" and remove the ? and everything after that. Thanks
Excellent. That works! I tried removing the slash between the last two groups (restaurant) and ([^?]+).* because I would like it to match /seattle/restaurant?page=2 in the same way as /seattle/restaurant/sushi?page=2. But I couldn't get it to work. Since I have a lot of other URLs on the 2. level position like /seattle/restaurant-michelangelo, which should NOT be caught, I guess I need to hardcode the values on the 2. level which SHOULD be caught (eg. restaurant, cafe, take-away). At the third level (/seattle/restaurant/sushi) I don't have the problem, so your suggestion works here. Thanks
How do I modify the regex to match seattle and restaurant from the url /seattle/restaurant? Page =2 and discard the querystring ? I tried modifying the above but could not get it to work. Thanks
@ThomasD: I recommend you open a new question.
thanks. I opened a new question at stackoverflow.com/questions/9145069/need-help-modifying-regex. Thank you for your help. I really appreciate it
1

You should change your final /./ to match "anything but a question mark" like this

^~/(.*)/restaurant/([^?]*)

1 Comment

Just tried it, and it partially works :-) I would still like it to return "sushi" even though "sushi" might be followed by a querystring. So eg a query like /seattle/restaurant/sushi?page=2 would match "seattle" and "sushi". Right now it seems it doesn't match "sushi" if it is proceeded by a querystring. Thanks

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.