[issue1761] Bug in re.sub()

Fredrik Lundh report at bugs.python.org
Wed Jan 9 14:30:20 CET 2008


Fredrik Lundh added the comment:

For the record, $ is defined to match "before a newline at the end of
the string, or at the end of the string" in normal mode, and "before any
newline, or at the end of the string" in multiline mode.

(and I have a vague memory that the "before a newline" behaviour in
normal mode was added for Perl compatibility)

> It seems that it matches BOTH the end of the string AND just before
the newline at the end of the string.

Of course it does: re.sub scans the string for matches from left to
right, and does the substitution everywhere the pattern matches, only
skipping over the matched parts.  Or in other words, if a pattern
matches N characters on position X has no influence on whether it
matches on position X+N or not.

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1761>
__________________________________


More information about the Python-bugs-list mailing list