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.