24

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)

5
  • 6
    How does it not work? Does it give an error? If so, post the error. Does it not match what you expect? If so, post the code where you use it and show the output you get vs. what you expect. Commented Dec 19, 2012 at 8:07
  • Here is the doc for the re module, clearly state support of negative lookbehind assertions. Commented Dec 19, 2012 at 8:17
  • 3
    Negative lookbehinds work in re as long as all alternatives have the same length. So this works (?<!asp|php|jsp), but not this (?<!asp|php|html). Commented Dec 19, 2012 at 8:28
  • @georg how would you accomodate for different length strings with lookbehinds? Commented Oct 26, 2017 at 21:11
  • 1
    @physlexic: regex (pypi.python.org/pypi/regex) can do this Commented Oct 27, 2017 at 8:42

1 Answer 1

26

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)\?.*
Sign up to request clarification or add additional context in comments.

1 Comment

... and re.search(r"(?<!(asp|php|jsp))\?.*", s).group() gives '?1=123'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.