[Tutor] Regex puzzlement

Bill Mill bill.mill at gmail.com
Sun Aug 8 20:57:57 CEST 2004


from the re documentation
(http://www.python.org/doc/current/lib/re-syntax.html):
    If you want to include a "]" or a "-" inside a set, precede it
with a backslash

What happened is that the '-' was interpreted by the regex module as
meaning that you had presented a character range from '\\' to ';'.

In [31]: ord('\\')
Out[31]: 92

In [32]: ord(';')
Out[32]: 59

In [33]: ord('1')
Out[33]: 49

In [34]: ord('a')
Out[34]: 97

Thus, anything with an ord between 92 and 59 would be interpreted as
matching your regex.

Peace
Bill Mill

On Sun, 8 Aug 2004 19:10:43 +0100, Alan Gauld
<alan.gauld at blueyonder.co.uk> wrote:
> My turn to be confused by a regex...
> 
> >>> import re
> >>> r = re.compile('[&()-;:,.?!]')
> >>> r.findall('Here is one, or two. but not 6 or 7')
> [',', '.', '6', '7']
> >>>
> 
> Why is it finding the numbers?
> Presumably some weird regex convention amongst the chars I've
> put in the group, but what? And how do I get rid of it?
> 
> Alan G.
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list