regex doubts

John Machin sjmachin at
Sun Jul 20 23:12:10 CEST 2008

On Jul 21, 12:30 am, Fredrik Lundh <fred... at> wrote:
> John Machin wrote:
> >> try "[LRM]+$" (an L or an R or an M, one or more times, all the way to
> >> the end of the string).
> > Ummm ... with the default flag settings, shouldn't that be \Z instead
> > of $ ?
> Why?  The OP was reading input from a user; whether he gets a trailing
> newline or not depends on the input method, and $ does the right thing
> for all normal input methods.

The goal as far as I can tell was to produce a pattern that matched
one (maybe zero) or more instances of 'L', 'R', or 'M', and no other

>>> bool(re.match(r'[LRM]+\Z', 'MRL\n'))
>>> bool(re.match(r'[LRM]+$', 'MRL\n'))

'\n' is an "other character".

Perhaps you could explain what you mean by "$ does the right thing".

More information about the Python-list mailing list