handling html links

Haris lekatsas at ee.princeton.edu
Thu Jan 24 00:47:35 EST 2002


Hi,

The following piece of code:

p = re.compile(r'((ftp|http)://[\w-]+(?:\.[\w-]+)*(?:/[\w-\?=\.&~]*)*)')
htmlized_str = p.sub('<a href="\\1">\\1</a>',original_string)

would work fine in Python 1.5.2, and would take a string that contains
...http://www.google.com... and would convert it to <a
href="http://www.google.com">http://www.google.com</a>
with no problems. I recently installed Python 2.1.1 and this does not
work anymore, in fact the compile command raises an exception:

Traceback (most recent call last):
  File "C:\Python21\test.py", line 8, in ?
    original()
  File "C:\Python21\test.py", line 5, in original
    p = re.compile(r'((ftp|http)://[\w-]+(?:\.[\w-]+)*(?:/[\w-\?=\.&~]*)*)')
  File "C:\PYTHON21\lib\sre.py", line 90, in compile
    return _compile(pattern, flags)
  File "C:\PYTHON21\lib\sre.py", line 136, in _compile
    raise error, v # invalid expression
error: bad character range

Does anyone know what is the problem here?

Thanks
Haris



More information about the Python-list mailing list