[Tutor] Regex puzzlement
alan.gauld at blueyonder.co.uk
Mon Aug 9 00:27:44 CEST 2004
Doh! Its always obvious once you see it!
Thanks Bill, I knew it had to be something abouyt one of the
characters but short of escaping each one in turn... I knew
about the range thing, just didn't see it.
----- Original Message -----
From: "Bill Mill" <bill.mill at gmail.com>
To: "Alan Gauld" <alan.gauld at blueyonder.co.uk>
Cc: <tutor at python.org>
Sent: Sunday, August 08, 2004 8:03 PM
Subject: Re: [Tutor] Regex puzzlement
> Hey, that last message didn't make sense. What I meant was that it
> interpreted as a character range from ')' to ';', where:
> In : ord(')')
> Out: 41
> In : ord(';')
> Out: 59
> In : ord('1')
> Out: 49
> In : ord('a')
> Out: 97
> so anything with an ord between 41 and 59 matches your regex.
> Bill Mill
> On Sun, 8 Aug 2004 14:57:57 -0400, Bill Mill <bill.mill at gmail.com>
> > 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
> > meaning that you had presented a character range from '\\' to ';'.
> > In : ord('\\')
> > Out: 92
> > In : ord(';')
> > Out: 59
> > In : ord('1')
> > Out: 49
> > In : ord('a')
> > Out: 97
> > Thus, anything with an ord between 92 and 59 would be interpreted
> > 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