问题
I am new to Python and not a full time coder as well. I am trying to see if there are any inbuilt regex functions in python that will provide the entire (expanded) list of strings from a python regex.
Example: for the input:
Good (Morning|afternoon|evening) my (friends|family|brothers|sisters). hope you like (apple|orange|grape) juice
The outputs should be:
Good Morning my friends. hope you like apple juice
Good Morning my friends. hope you like orange juice
Good Morning my friends. hope you like grape juice
Good Morning my family. hope you like apple juice
Good Morning my family. hope you like orange juice
Good Morning my family. hope you like grape juice
Good Morning my brothers. hope you like apple juice
Good Morning my brothers. hope you like orange juice
Good Morning my brothers. hope you like grape juice
Good Morning my sisters. hope you like apple juice
Good Morning my sisters. hope you like orange juice
Good Morning my sisters. hope you like grape juice
Good afternoon my friends. hope you like apple juice
Good afternoon my friends. hope you like orange juice
Good afternoon my friends. hope you like grape juice
Good afternoon my family. hope you like apple juice
Good afternoon my family. hope you like orange juice
Good afternoon my family. hope you like grape juice
Good afternoon my brothers. hope you like apple juice
Good afternoon my brothers. hope you like orange juice
Good afternoon my brothers. hope you like grape juice
Good afternoon my sisters. hope you like apple juice
Good afternoon my sisters. hope you like orange juice
Good afternoon my sisters. hope you like grape juice
Good evening my friends. hope you like apple juice
Good evening my friends. hope you like orange juice
Good evening my friends. hope you like grape juice
Good evening my family. hope you like apple juice
Good evening my family. hope you like orange juice
Good evening my family. hope you like grape juice
Good evening my brothers. hope you like apple juice
Good evening my brothers. hope you like orange juice
Good evening my brothers. hope you like grape juice
Good evening my sisters. hope you like apple juice
Good evening my sisters. hope you like orange juice
Good evening my sisters. hope you like grape juice
It doesn't matter in which order it is populated. As long as I have the output in a list, that's ok.
回答1:
You could use Exrex. Install with pip install exrex
.
Then execute in terminal:
exrex 'Good (Morning|afternoon|evening) my (friends|family|brothers|sisters)\. hope you like (apple|orange|grape) juice\.'
Make sure not to forget the backslashes \
before the dots .
, as dots are a special character inside regexes. This will return:
Good Morning my friends. hope you like apple juice.
Good Morning my friends. hope you like orange juice.
Good Morning my friends. hope you like grape juice.
Good Morning my family. hope you like apple juice.
Good Morning my family. hope you like orange juice.
Good Morning my family. hope you like grape juice.
Good Morning my brothers. hope you like apple juice.
Good Morning my brothers. hope you like orange juice.
Good Morning my brothers. hope you like grape juice.
Good Morning my sisters. hope you like apple juice.
Good Morning my sisters. hope you like orange juice.
Good Morning my sisters. hope you like grape juice.
Good afternoon my friends. hope you like apple juice.
Good afternoon my friends. hope you like orange juice.
Good afternoon my friends. hope you like grape juice.
Good afternoon my family. hope you like apple juice.
Good afternoon my family. hope you like orange juice.
Good afternoon my family. hope you like grape juice.
Good afternoon my brothers. hope you like apple juice.
Good afternoon my brothers. hope you like orange juice.
Good afternoon my brothers. hope you like grape juice.
Good afternoon my sisters. hope you like apple juice.
Good afternoon my sisters. hope you like orange juice.
Good afternoon my sisters. hope you like grape juice.
Good evening my friends. hope you like apple juice.
Good evening my friends. hope you like orange juice.
Good evening my friends. hope you like grape juice.
Good evening my family. hope you like apple juice.
Good evening my family. hope you like orange juice.
Good evening my family. hope you like grape juice.
Good evening my brothers. hope you like apple juice.
Good evening my brothers. hope you like orange juice.
Good evening my brothers. hope you like grape juice.
Good evening my sisters. hope you like apple juice.
Good evening my sisters. hope you like orange juice.
Good evening my sisters. hope you like grape juice.
来源:https://stackoverflow.com/questions/55267366/expand-python-regex-to-list-of-all-possible-strings