[Python-bugs-list] [ python-Bugs-441600 ] Misleading doc of $ metacharacter in re

noreply@sourceforge.net noreply@sourceforge.net
Sun, 15 Jul 2001 22:29:24 -0700


Bugs item #441600, was opened at 2001-07-15 22:29
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=441600&group_id=5470

Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Nobody/Anonymous (nobody)
Summary: Misleading doc of $ metacharacter in re 

Initial Comment:
From: machin_john_888@hotmail.com (John Machin)
Newsgroups: comp.lang.python
Subject: Re: re Challenge: More Compact?
References: <mailman.995247446.16415.python-
list@python.org>
NNTP-Posting-Host: 140.168.16.251
Message-ID: 
<92ae279c.0107152124.4c049d5e@posting.google.com>

"Tim Peters" <tim.one@home.com> wrote in message 
news:<mailman.995247446.16415.python-
list@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 $).



----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=441600&group_id=5470