如何组合多个python regex参数?

2024-04-29 00:51:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从列表中筛选出以下内容:

^[^\w ]$

'^\n( )*$' (\n at the beginning followed by spaces)

'^\n( )*\n$' (\n at both ends with spaces)

'^[]$' (space alone)

如何将它们组合成一个表达式?你知道吗


Tags: the列表by表达式withspaceatspaces
1条回答
网友
1楼 · 发布于 2024-04-29 00:51:03

试试这个:

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)

相关问题 更多 >