0

For a string "I have a dog, a fish, and a cat", I would like to capture the groups in the order "dog", "fish", and "cat".

I have a Python regex that works the way I want, making the groups optional in case the string doesn't contain the groups. So "I have a dog and a cat" would still give me groups of "dog" and "cat".

^(?:.*(dog))?(?:.*(fish))?(?:.*(cat))?.*$

However I would like to capture the groups regardless of the orders of the groups in the regex. If the string is "I have a fish, a dog, and a cat", I only get the groups "dog" and "cat" when I would still like "dog", "fish", and "cat"

I originally used lookaheads with capture groups to ignore the order, but that only works if all of the groups are in the string. I've tried combining lookaheads with non-capture groups but can't seem to make it work.

Any help would be appreciated!

Here's a link to my regex: https://regex101.com/r/lhT55K/2

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
mmille
  • 54
  • 3

0 Answers0