Ant antroy at
Mon Jan 19 16:01:02 CET 2009

A 0-width positive lookahead is probably what you want here:

>>> s = """
... hdhd <a href="">Test <i>String</i> OK</
... """
>>> p = r'href="([^"]+)">(.*)(?=</a>)'
>>> m =, s)
'Test <i>String</i> OK'

The (?=...) bit is the lookahead, and won't consume any of the string
you are searching. I've binned the named groups for clarity.

The beautiful soup answers are a better bet though - they've already
done the hard work, and after all, you are trying to roll your own
partial HTML parser here, which will struggle with badly formed html...

More information about the Python-list mailing list