我想将 or
与我获得的字符串进行匹配,并根据 or
出现的次数对结果进行分组。
我的输入如下:
a or b*~c or 27*y or 5*~b
所以我的输出应该是:
a, b*~c, 27*y, 5*~b
如果正好有 3 个 or
,我的代码就可以工作,否则返回 []
。
我是 python 新手,我不明白如何将模式提供给编译函数。
import re
input = raw_input(" ")
ans = re.compile(r'(.*) or (.*) or (.*) or (.*)')
print re.findall(ans, input)
最佳答案
只需根据子字符串或
进行拆分
re.split(r' or ', s)
或
re.split(r'\s+or\s+', s)
关于Python 使用正则表达式对相似模式进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33429399/