Boy do REs ever suck sometimes...
Richard Jones
richard at bizarsoftware.com.au
Fri Oct 5 03:07:26 EDT 2001
On Friday 05 October 2001 16:58, Tim Peters wrote:
> [Ignacio Vazquez-Abrams]
>
> > Here's the problem I'm having with REs. When I use an RE like
> > '^[0-9]+$' it matches both '123' and '123\n'. How can I get it to not
> > match the string with the newline?
>
> A straightforward way:
> >>> import re
> >>> pat = re.compile(r'\d+\Z')
> >>> pat.match('123')
>
> <SRE_Match object at 0x007AC8C0>
Ahhh... I didn't know about \Z ... the subtlety lying in the Library
Reference description of the two:
'$' "Matches the end of the string ..." (goes on to talk about MULTILINE)
'\Z' "Matches only at the end of the string."
Hrm - perhaps the ref could have a note in it to explain that "$" will do
some sort of automagic lookahead for a newline, and that if you don't want
that, then use '\Z'?
Richard
More information about the Python-list
mailing list