Let's say we have the following string:
1|order=asc|type=1
We need to create regex that will parse it to parameters assuming that string always should start with number and will (optionally) have parameters (order, asc) in any order. Also it may have 3 and more parameters but let's keep 2 here for simplicity. For instance these are the string that regex will understand:
1
1|order=asc|type=1
1|type=1|order=asc
I have the following expression that does this job:
(?<id>^\w+)((?:\|type=(?<type>\w+))|(?:\|order=(?<order>\w+))){0,2}
This is demo link to my regex
But the issue is that it allows duplicates.
If we have the following string it will not match order
parameter at all:
1|type=1|type=2|order=asc
Ideally we should have the following groups from regex above:
You can use
^(?<id>\w+)(?:\|(?:type=(?<type>\w+)|order=(?<order>\w+)))*$
See the regex demo.
Details:
^
- string start(?<id>\w+)
- Group "id": one or more word chars(?:\|(?:type=(?<type>\w+)|order=(?<order>\w+)))*
- zero or more repetitions of
\|
- a |
char(?:type=(?<type>\w+)|order=(?<order>\w+))
- either of
type=(?<type>\w+)|
- type=
text, then Group "type" capturing one or more word chars, and then an OR operatororder=
- text order=
and then(?<order>\w+)
- Group "order": one or more word chars$
- string end.The capturing groups inside the (?:...)*
repeated group will keep re-writing the group values each time they capture a string and thus the last occurrences will be kept.
You can enhance this regex by adding more groups inside the second non-capturing group. For example, adding num
and status
is as easy as
^(?<id>\w+)(?:\|(?:type=(?<type>\w+)|order=(?<order>\w+)|num=(?<num>\d+)|status=(?<status>\w+)))*$
See this regex demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With