How to parse for tags with '+' in python

后端 未结 2 1064
一个人的身影
一个人的身影 2021-01-03 04:30

I\'m getting a \"nothing to repeat\" error when I try to compile this:

search = re.compile(r\'([^a-zA-Z0-9])(%s)([^a-zA-Z0-9])\' % \'+test\', re.I)
         


        
2条回答
  •  伪装坚强ぢ
    2021-01-03 04:38

    re.compile(r'([^a-zA-Z0-9])(%s)([^a-zA-Z0-9])' % '\+test', re.I)
    

    The "+" is the "repeat at least once" quantifier in regular expressions. It must follow something that is repeatable, or it must be escaped if you want to match a literal "+".

    Better is this, if you want to build your regex dynamically.

    re.compile(r'([^a-zA-Z0-9])(%s)([^a-zA-Z0-9])' % re.escape('+test'), re.I)
    

提交回复
热议问题