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:
- id:1
- type:2 (as it should capture last occurrence)
- order:asc