[Python-ideas] string.replace should accept a list as a first argument

Random832 random832 at fastmail.com
Wed Oct 7 04:14:54 CEST 2015


Chris Angelico <rosuav at gmail.com> writes:

> On Wed, Oct 7, 2015 at 11:49 AM, Random832 <random832 at fastmail.com> wrote:
>> Not sure how to get around that, other than by creating a general
>> "structured regex" module, to build a compiled regex from an abstract
>> regex syntax tree rather than a string. Which actually might not be the
>> worst thing in the world. I think there are some Lisp dialects that have
>> something like that.
>
> Ooh, I like this idea. Kinda like the difference between building up
> an SQL statement by escaping all your input, and using a parameterized
> statement.

Not really. My idea was that no string in regex syntax ever exists, but
instead something like "(?a|b|c)d*" becomes more like this:
Sequence(Alternate("a","b","c"), Star("d")), which the regex engine
could then use instead of parsing a string to build a state machine.
All strings would be literal strings - "a\.b" would become "a.b" and
"a.b" would become Sequence("a", Dot(), "b")

Then you could just use Alternate(*lst).


More information about the Python-ideas mailing list