这个正则表达式(?<!(asp|php|jsp))\?.*
在PCRE中有效,但在Python中却无法工作。
那么我该如何使这个正则表达式在Python 2.7中生效呢?
这个正则表达式(?<!(asp|php|jsp))\?.*
在PCRE中有效,但在Python中却无法工作。
那么我该如何使这个正则表达式在Python 2.7中生效呢?
对我来说它完全正常运行。 也许你在错误地使用它? 确保使用 re.search
而不是 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>
这正是您的模式应该表现的方式。就像glglgl提到的那样,如果您将Match
对象分配给一个变量(比如m
),然后调用m.group()
,就可以得到匹配结果?1=123
。
顺便说一下,你可以省略内部括号。这个模式等价于:
(?<!asp|php|jsp)\?.*
re.search(r"(?<!(asp|php|jsp))\?.*", s).group()
则返回'?1=123'
。 - glglgl
(?<!asp|php|jsp)
是可以使用的,但(?<!asp|php|html)
不行。 - georg