python中的多次重复错误

hae*_*ney 2 python regex

我正在尝试在python中使用一些正则表达式

re.compile('in versions: (.+?) of '+name+' ')
Run Code Online (Sandbox Code Playgroud)

并且,如果名称为“ libcrypto ++”,则导致多次重复错误

我怎么只能在字符串中处理它?

Ale*_*rov 6

+是正则表达式中的量词。因此,当您添加libcrypto++到正则表达式字符串时,它会将其中两个并排在一起是没有意义的。看到这个。

为了解决这个问题,您可以使用正则表达式转义方法,例如:

re.compile('in versions: (.+?) of '+ re.escape(name) +' ')
Run Code Online (Sandbox Code Playgroud)