Following the pattern from this article, I’m trying to create a regular expression that would capture some groups in arbitrary order.
/(?=.*(?<one>foo))(?=.*(?<two>bar))(?=.*(?<three>baz))/
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 at Regex101. Would appreciate any advice.
3
Answers
Why not just use a global flag?
You can use non-greedy quantifiers for the wildcards:
Demo: https://regex101.com/r/Tiw8wu/1
You can try this regex
working demo with explaination