/(?=.*(?<one>foo))(?=.*(?<two>bar))(?=.*(?<three>baz))/
Following the pattern from this article, I’m trying to create a regular expression that would capture some groups in arbitrary order. However, the additional requirement is that the groups must be optional, i.e. it must match all the strings below:
foo bar baz
foo baz bar
baz bar foo
foo bar
foo baz
bar baz
foo
bar
baz
I tried applying a quantifier ?
to the capturing group but for some reason it stops capturing that group completely. I’m wondering if that’s possible at all?
/(?=.*(?<one>foo)?)(?=.*(?<two>bar)?)(?=.*(?<three>baz)?)/
Here’s the demo.
Would appreciate any advice.