[Python-Dev] Missing API in re modle

Guido van Rossum guido@beopen.com
Tue, 19 Sep 2000 17:47:11 -0500


When investigating and fixing Tim's report that the Replace dialog in
IDLE was broken, I realized that there's an API missing from the re
module.

For search-and-replace, IDLE uses a regular expression to find the
next match, and then needs to do whatever sub() does to that match.
But there's no API to spell "whatever sub() does"!  It's not safe to
call sub() on just the matching substring -- the match might depend on
context.

It seems that a new API is needed.  I propose to add the following
method of match objects:

  match.expand(repl)

    Return the string obtained by doing backslash substitution as for
    the sub() method in the replacement string: expansion of \n ->
    linefeed etc., and expansion of numeric backreferences (\1, \2,
    ...) and named backreferences (\g<1>, \g<name>, etc.);
    backreferences refer to groups in the match object.

Or am I missing something and is there already a way to do this?

(Side note: the SRE code does some kind of compilation on the
replacement template; I'd like to see this cached, as otherwise IDLE's
replace-all button will take forever...)

--Guido van Rossum (home page: http://www.pythonlabs.com/~guido/)