[issue14221] re.sub backreferences to numbered groups produce garbage

Phillip Feldman report at bugs.python.org
Wed Mar 7 18:28:34 CET 2012


New submission from Phillip Feldman <phillip.m.feldman at gmail.com>:

The first example below works; the second one produces output containing garbage characters.  (This came up while I was creating a set of examples for a tutorial on regular expressions).

import re

text= "The cat ate the rat."
print("before: %s" % text)
m= re.search("The (\w+) ate the (\w+)", text)
text= "The %s ate the %s." % (m.group(2), m.group(1))
print("after : %s" % text)

text= "The cat ate the rat."
print("before: %s" % text)
text= re.sub("(\w+) ate the (\w+)", "\2 ate the \1", text)
print("after : %s" % text)

----------
components: Regular Expressions
messages: 155100
nosy: Phillip.M.Feldman, ezio.melotti, mrabarnett
priority: normal
severity: normal
status: open
title: re.sub backreferences to numbered groups produce garbage
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14221>
_______________________________________


More information about the Python-bugs-list mailing list