New submission from Phillip Feldman <phillip.m.feld...@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 <rep...@bugs.python.org> <http://bugs.python.org/issue14221> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com