While it's impossible most of the time to derive all possible text that could match regexes, I'm wondering about a specific case where you have I (like|hate) (pizza|ice cream)!
This would result in four possibilities:
I like ice cream!
I like pizza!
I hate ice cream!
I hate pizza!
Now, it's not bad to homebrew something to split this up.
import re
my_string = "I (like|hate) (ice cream|pizza)!"
def recursive_possibilities(my_list):
print("Recursing on", my_list)
my_new_list = []
for m in my_list:
checklist = re.findall(r"\([a-z \|]+\)", m)
if len(checklist) == 0:
my_new_list.append(m)
continue
x = checklist[0]
for y in x[1:-1].split("|"):
my_new_list.extend(recursive_possibilities([m.replace(x, y)]))
return my_new_list
for r in recursive_possibilities([my_string]):
print(r)
I get the feeling
- my code could be better and
- someone else has done it better, and before me, and put it (and other stuff I'd like later) in a module.
Any ideas where I can look for this? Thanks!