re Challenge: More Compact?

John Machin machin_john_888 at hotmail.com
Mon Jul 16 05:43:44 EDT 2001


"Tim Peters" <tim.one at home.com> wrote in message news:<mailman.995264665.17334.python-list at python.org>...
> [John Machin, on $ matching before or after a terminating newline]
> > ...
> > I shall enter a bug report and leave it up to the powers_that_be to
> > decide whether to change the behaviour or section 4.2.1 of the
> > documentation.
> 
> The behavior can't be changed:  this was deliberate, for compatibility with
> Perl5 regexps.  Mounds of code would break if it changed.

That's what I feared. Then the sad-but-true story that's revealed
about $ in 4.2.3 should be told in 4.2.1 which is what people see
first. [I'm sure lots of people are like me -- never had occasion to
use "multline" mode and thus never bothered reading 4.2.3.] Perhaps it
should also mention that if they don't like the idea of pattern "abc$"
matching both strings "abc" and "abc\n", then they can use \Z ...

> 
> > To answer Tim's question, yes it bothers me ... the solution of course
> > always when you want to validate that the pattern matches the whole
> > string (and not some leading substring) is to have \Z at the end of
> > your pattern (not $).
> 
> Careful!

Not sure what you mean here; who should be careful of what? Pattern
"abc\Z" matches string "abc" but does not match strings "abcdef" and
"abc\n" -- just what one wants in a "whole string and nothing but the
string" match. After looking up Friedl's book and being aghast at what
Perl did with \Z, I was very careful to check that Python did it
"properly" before posting. The advice given is correct for Python. I
don't feel any duty of care towards any stray Perl aficionados who
might be reading this news-group :-)

> This is where Python and Perl parted company, and again
> deliberately.  Guido and Larry argued about this near the end of '97, and
> Python refused to let \Z match before a terminating newline; Perl does.
> Perl later grew \z to do what Python's (but not Perl's) \Z does.
> 
> Three years later, in
> 
> http://archive.develooper.com/perl6-language-regex@perl.org/msg00358.html
> 
> Tom Christiansen wrote:
> 
>     This is an annoying gotcha.  Larry once said that he wished
>     he had made \Z do what \z now does.  One would like $ to (be
>     able to) mean "ONLY AT END OF STRING".
> 
> Heh -- *everyone* should listen to Guido <wink>.

Good thing Larry never got to collaborate with Grace Hopper ... ALTER
$_ TO MEAN SOMETHING-ELSE DEPENDING ON $*

Cheers,
John



More information about the Python-list mailing list