Expand Python regex to list of all possible strings [closed]

柔情痞子 提交于 2019-12-13 11:25:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!