如何组合多个 python 正则表达式参数



我需要从列表中过滤掉以下内容:

^[^w ]$
'^n( )*$' (n at the beginning followed by spaces)
'^n( )*n$' (n at both ends with spaces)
'^[]$' (space alone)

如何将它们组合成一个表达式?

试试这个:

r1 ='^[^w ]'
r2 ='^n( )*'
r3= '^n( )*n'
r4 = '^n( )*n'
string="(n at the beginning followed by spaces)"
generic_re = re.compile("(%s|%s|%s|%s)" % (r1, r2, r3, r4)).findall(string)

最新更新