regex doubts

John Machin sjmachin at lexicon.net
Sun Jul 20 17:12:10 EDT 2008


On Jul 21, 12:30 am, Fredrik Lundh <fred... at pythonware.com> 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
characters.

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

'\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