re Challenge: More Compact?

John Machin machin_john_888 at hotmail.com
Mon Jul 16 01:24:42 EDT 2001


"Tim Peters" <tim.one at home.com> wrote in message news:<mailman.995247446.16415.python-list at python.org>...
> BTW, does it bother anyone that all of these solutions accept an IP ending
> with a newline?
> 
> regexps-are-surprising-ly y'rs  - tim

Straight out of the horse's mouth at
http://python.sourceforge.net/devel-docs/lib/re-syntax.html
(this is in section 4.2.1 of the Python Library Reference):
<quote>
"$" 
Matches the end of the string, and in MULTILINE mode also matches
before a newline. foo matches both 'foo' and 'foobar', while the
regular expression foo$ matches only 'foo'.
</quote>
But the real story only emerges two pages later (in section 4.2.3):
<quote>
M 
MULTILINE
When specified, the pattern character "^" matches at the beginning of
the string and at the beginning of each line (immediately following
each newline); and the pattern character "$" matches at the end of the
string and at the end of each line (immediately preceding each
newline). By default, "^" matches only at the beginning of the string,
and "$" only at the end of the string and immediately before the
newline (if any) at the end of the string.
</quote>

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.

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 $).



More information about the Python-list mailing list