Raw string substitution problem

MRAB python at mrabarnett.plus.com
Fri Dec 18 12:17:27 EST 2009


Gregory Ewing wrote:
> MRAB wrote:
> 
>> Regular expressions and replacement strings have their own escaping
>> mechanism, which also uses backslashes.
> 
> This seems like a misfeature to me. It makes sense for a regular
> expression to give special meanings to backslash sequences, because
> it's a sublanguage with its own syntax. But I can't see any earthly
> reason to do that with the *replacement* string, which is just data.
> 
> It looks like a feature that's been blindly copied over from Perl
> without thinking about whether it makes sense in Python.
> 
In simple cases you might be replacing with the same string every time,
but other cases you might want the replacement to contain substrings
captured by the regex.

For example, swapping pairs of words:

>>> re.sub(r'(\w+) (\w+)', r'\2 \1', r'first second third fourth')
'second first fourth third'


Python also allows you to provide a function that returns the
replacement string, but that seems a bit long-winded for those cases
when a simple replacement template would suffice.



More information about the Python-list mailing list