[Tutor] Regex puzzlement
bill.mill at gmail.com
Sun Aug 8 20:57:57 CEST 2004
from the re documentation
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 : ord('\\')
In : ord(';')
In : ord('1')
In : ord('a')
Thus, anything with an ord between 92 and 59 would be interpreted as
matching your regex.
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
More information about the Tutor