[Python-ideas] Give regex operations more sugar

Franklin? Lee leewangzhong+python at gmail.com
Wed Jun 13 20:59:58 EDT 2018

On Wed, Jun 13, 2018 at 8:15 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Chris Angelico wrote:
>> This would want to be semantically different from chained
>> calls, in that a single replace([x,y,z], q) would avoid re-replacing;
> +1, this would be REALLY handy!
> It's easy to trip yourself up with chained replacements
> if you're not careful -- like I did once when escaping
> things using &xxx; sequences in XML. If you don't do it
> in the right order, you end up escaping some of the &s
> you just inserted. :-(
> --
> Greg

In a thread earlier this year, I suggested allowing a dict:

For example:
            '&': '&',
            '"': '"',
            "'": ''',

Tuples of strings can be dict keys, so it's also possible to allow
several options to be replaced with a single thing.

One use I had for multi-replace was to parse a file that was almost
CSV, but not close enough to be parsed by `import csv`. I had to be
careful to get the order right so that old replacements wouldn't cause
newer ones.

More information about the Python-ideas mailing list