1

I am having hard time learning regex and honestly I have no time at the moment. I am looking for a regex expression that would match url route with query string

What I need is regex to match population?filter=nation of course where nation can be any string.

Based on my current regex knowledge I have also tried with regex expression /^population\/(?P<filterval>\d+)\/filter$/ to match population/nation/filter but this does not work.

Any suggestion and help is welcome.

4
  • 1
    Perhaps, ^\/?population(?:\/|\?filter=)(?P<filterval>[^\/]+)(?:\/filter\/?)?$ (see demo)? Commented Jul 7, 2015 at 9:48
  • 2
    Wait ... which expression do you want to match? the first or the second one? Commented Jul 7, 2015 at 9:48
  • @Serv Ideally first second one was attempt based on my regex knowledge. Commented Jul 7, 2015 at 9:57
  • Can nation values contain characters like -? Like in "Great-Britain" or "New-Zealand" just for the sake of being there. Not being gramatically correct and is this the complete querystring or does something else follow? (filter=nation&foo=bar) ? Commented Jul 7, 2015 at 10:02

1 Answer 1

1

This does match only your first query string format:

population\?filter=[\w]+[-_]?[\w]+

Addiotionally it allows for - and _ as bindings between words. If you know, that your string ends right there, you can also add an $ to the end to mark it so.

If you know that the nation is only alphabetical characters, yu can use the simplified version:

 population\?filter=[\w]+

Demo

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

1 Comment

I had to add forward slashes at the beginning and end '/population\?filter=[\w]+[-_]?[\w]+/' because it reported "Warning: preg_match(): Delimiter must not be alphanumeric or backslash in ..." Beside that this was very helpful! 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.