Python Regex Negative Lookbehind

The pattern (?<!(asp|php|jsp))\?.* Works in PCRE, but it does not work in Python.

So what can I do to get this regular expression work in Python? (Python 2.7)

+8
python regex pcre negative-lookbehind
source share
1 answer

This works great for me. Perhaps you are using it incorrectly? Be 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> 

This is how your template should behave. As mentioned in glglgl, you can get a match if you assign this Match object to a variable (say m ) and then call m.group() . This gives ?1=123 .

By the way, you can leave the inner brackets. This pattern is equivalent to:

 (?<!asp|php|jsp)\?.* 
+9
source share

All Articles