dushkin
dushkin

Reputation: 2101

HTTP query parameters regular expression

Probably a minor one for the pros...

I want to detect a string of query parameters like:

var1=val1&var2=val2&...

I tried:

((.*)?(#[\w\-]+)?$&*)*

which is fine except it also matches

var1=val1&

which I don't want.


EDIT:

Tried also this:

(.*)=(.*)&*

is it a good solution?

Upvotes: 0

Views: 52

Answers (2)

Try the following,

(?:&?[^=&]*=[^=&]*)+

You can test it from here as well as its description.


Tried also this:

(.*)=(.*)&*

is it a good solution?

No. What about the input string is something like var1=val1&var2=val2&...sdsad3423--**909 ?

Upvotes: 1

Gurwinder Singh
Gurwinder Singh

Reputation: 39477

Try this:

^([^=]+=[^&]*&)*([^=]+=[^&]*)$

Here, following matches one key-value pair.

[^=]+=[^&]*

So, zero or more key-value pairs each ending with &:

([^=]+=[^&]*&)*

And, one key-value pair at the end with no following &:

([^=]+=[^&]*)

Regex101

Upvotes: 1

Related Questions