Python Regex Negative Lookbehind

 ̄綄美尐妖づ 提交于 2020-12-04 15:58:39

问题


The pattern (?<!(asp|php|jsp))\?.* works in PCRE, but it doesn't work in Python.

So what can I do to get this regex working in Python? (Python 2.7)


回答1:


It works perfectly fine for me. Are you maybe using it wrong? Make sure to use re.search instead of re.match:

>>> import re
>>> s = 'somestring.asp?1=123'
>>> re.search(r"(?<!(asp|php|jsp))\?.*", s)
>>> s = 'somestring.xml?1=123'
>>> re.search(r"(?<!(asp|php|jsp))\?.*", s)
<_sre.SRE_Match object at 0x0000000002DCB098>

Which is exactly how your pattern should behave. As glglgl mentioned, you can get the match if you assign that Match object to a variable (say m) and then call m.group(). That yields ?1=123.

By the way, you can leave out the inner parentheses. This pattern is equivalent:

(?<!asp|php|jsp)\?.*


来源:https://stackoverflow.com/questions/13947933/python-regex-negative-lookbehind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!