I am writing a negative lookbehind assertion expression in Python which performs the following function to parse a plain text file:

Does not match anything followed after http://********** ; but will match the pattern when it is not inside a http://* link

http://www.test.com/aa4   cd6
bx2 vq9 
yu9 http://www.bh9.com/cj3

Matches: cd6,bx2,vq9 and yu9

So I tried regexps like


They did not work.

How to add .* or do similar opearion inside negative look behind assertion regex in Python.


Problem: Lookbehind does not allow pattern whose length is not fixed.

Quick hack: Perhaps the following regexp does the job?


It works like this:

>>> str = """http://www.test.com/aa4
... bx2 vq9 
... http://www.bh9.com/cj3
... """
>>> re.findall(r'(?<![./])[a-z][a-z][0-9]',str)
['bx2', 'vq9']

Or - as another solution - use a regexp matching urls to cut off all urls in your string and then search for r'[a-z][a-z][0-9]'

  • How does [./] perform the functionality? – thinkcool Jul 15 '11 at 8:12
  • @thinkcool: [./] matches a dot or a slash. I.e. the regexp presented in my answer matches all words [a-z][a-z][0-9] which do not follow a dot or a slash. – phynfo Jul 15 '11 at 8:45
  • what happens when the pattern just follows a [.] or [./] It will not match .fd3 . Is there any foolproof solution? – thinkcool Jul 15 '11 at 13:22
  • I am not able to understand your newly edited answer. Can you tell me how to proceed with this? – thinkcool Jul 17 '11 at 21:42

That not possible. Python allows only fixed length lookbehinds. That means no quantifier inside the lookbehind.

See here the feature list on egular-expressions.info

Your Answer

