python正则表达式匹配" OR"中的多个模式然后是一个模式

时间:2017-10-30 19:23:24

标签: python regex

我有一个长字符串,其中包含"年来自string.string"或者"来自string.string"。

的月份

现在我想匹配所有这些模式,我现在拥有的python代码是:

res = re.findall(r'(?ix)  year \s+ from \s+ [a-z0-9_]+[.][a-z0-9_]+ | month \s+ from \s+ [a-z0-9_]+[.][a-z0-9_]+ ', text)

我想知道如何以更简单的方式做到这一点。类似的东西:

res = re.findall(r'(?ix)  year | month \s+ from \s+ [a-z0-9_]+[.][a-z0-9_]+ ', text)

似乎没有工作,只是给了"年"但不是"年..."在结果中。

res = re.findall(r'(?ix) ( year | month ) \s+ from \s+ [a-z0-9_]+[.][a-z0-9_]+ ', text)

仅返回括号中的匹配项。

我问这个问题,因为这不是唯一的情况,可能有更长的模式,我需要匹配,我不想再次重复整个模式因为一部分字符串是不同的。

0 个答案:

没有答案