Validate URL query string with regex

expression, regex, validation

Solution

This seems to be what you want:

^\?([\w-]+(=[\w-]*)?(&[\w-]+(=[\w-]*)?)*)?$

See live demo

This considers each "pair" as a key followed by an optional value (which maybe blank), and has a first pair, followed by an optional `&` then another pair,and the whole expression (except for the leading`?`) is optional. Doing it this way prevents matching `?&abc=def`

Also note that hyphen doesn't need escaping when last in the character class, allowing a slight simplification.

You seem to want to allow hyphens anywhere in keys or values. If keys need to be hyphen free:

^\?(\w+(=[\w-]*)?(&\w+(=[\w-]*)?)*)?$

Problem

I'm trying to validate a query string with regex. Note that I'm not trying to match out the values, but validate its syntax. I'm doing this to practice regex, so I'd appreciate help rather than "use this lib", although seing how it may have been done in a lib would help me, so show me if you've got one. So, this would be the prerequisites: - It must start with a questionmark. - It may contain keys with or without values separated by an equals-sign, pairs separated by ampersand. I've got pretty far, but I'm having trouble matching in regex that the equals-sign and ampersand must be in a certain order without having to repeat match groups. This is what I've got so far: ``` #^\?([\w\-]+((&|=)([\w\-]+)*)*)?$# ``` It correctly matches `?abc=123&def=345`, but it also incorrectly matches for example `?abc=123=456`. I could go overkill and do something like... ``` /^\?([\w\-]+=?([\w\-]+)?(&[\w\-]+(=?[\w\-]*)?)*)?$/ ``` ... but I don't want to repeat the match groups which are the same anyway. How can I tell regex that the separators between values must iterate between `&` and `=` without repeating match groups or catastrophic back tracking? Thank you. Edit: I'd like to clarify that this is not meant for a real-world implementation; for that, the built-in library in your language, which is most likely available should be used. This question is asked because I want to improve my regex skills, and parsing a query string seemed like a rewarding challenge.

Original source