I know this question sounds similar to others, but see my notes below on those solutions.
I need a regex to use in Python to search for specific words in any order in a string. What I want to do is put brackets around the series of words.
Here is my example text:
apples, pears, bananas are fruit
pears, apples, and bananas are in the fruit basket
you can use bananas, pears, and apples in a fruit salad
he also likes pears, bananas, and apples for a snack
she likes cheese for a snack but also apples or pears
Ultimately, I want to be able to put brackets around the fruit series, like so:
[apples, pears, bananas] are fruit
[pears, apples, and bananas] are in the fruit basket
you can use [bananas, pears, and apples] in a fruit salad
he also likes [pears, bananas, and apples] for a snack
she likes cheese for a snack but also [apples or pears]
In doing my research, I found the following posts:
Multiple words in any order using regex
This solution did nothing when I tested it in Regex101.
Regex to match string containing two names in any order
This solution is very similar to the first and did not work either.
Regex to match multiple words in any order
This solution came the closest to working with some slight modification:
(APPLES|BANANAS|PEARS) \[\1]\
However, this puts brackets around each fruit listed rather than around the series:
[apples], [pears], [bananas] are fruit
I'm obviously missing something, so I would appreciate whatever help someone could give me.
Thanks!