regexp upward compatibility bug ?

G

Gilles Lenfant

Hi,

Porting a Zope app from (old) Zope 2.3.3 powered by Python 1.5.2 to a
newer Zope powered by Python 2.1.3, I found a regexp problem :

This does perfectly the job with Python 1.5.2 but raises an exception
with Python 2.1.3. I can't see what's wrong when reading the "re" module
doc.

Any hint welcome

File "txt2html.py", line 27, in txt2html
pat = re.compile(r'((ftp|http)://[\w-]+(?:\.[\w-]+)*(?:/[\w-\.?=]*)*)')
File "/usr/local/lib/python2.1/sre.py", line 90, in compile
return _compile(pattern, flags)
File "/usr/local/lib/python2.1/sre.py", line 136, in _compile
raise error, v # invalid expression
sre_constants.error: bad character range

Many thanks by advance
 
J

Jeff Epler

The problem is the use of '-' in the character groups, like
r'[\w-]'

Here's what the library reference manual has to say:
[]
Used to indicate a set of characters. Characters can be listed
individually, or a range of characters can be indicated by giving
two characters and separating them by a "-". Special characters are
not active inside sets. For example, [akm$] will match any of the
characters "a", "k", "m", or "$"; [a-z] will match any lowercase
letter, and [a-zA-Z0-9] matches any letter or digit. Character
classes such as \w or \S (defined below) are also acceptable inside
a range. If you want to include a "]" or a "-" inside a set, precede
it with a backslash, or place it as the first character. The pattern
[]] will match ']', for example.
http://www.python.org/doc/current/lib/re-syntax.html

So you may want to write r'[-\w]' or r'[\w\-]' instead, based on my
reading.

The same goes for the later part of the pattern [\w-\.?=].

Jeff
 
G

Gilles Lenfant

Jeff Epler said:
The problem is the use of '-' in the character groups, like
r'[\w-]'
[...]

Jeff

Jeff,

Many thanks for the time you spent for my enlightenments. I fixed the stuff
with your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top