What is built-in method sub

Chris Rebert clp2 at rebertia.com
Mon Jan 11 16:51:48 EST 2010


On Mon, Jan 11, 2010 at 12:34 PM, Steven D'Aprano
<steve at remove-this-cybersource.com.au> wrote:
<snip>
> If you can avoid regexes in favour of ordinary string methods, do so. In
> general, something like:
>
> source.replace(target, new)
>
> will potentially be much faster than:
>
> regex = re.compile(target)
> regex.sub(new, source)
> # equivalent to re.sub(target, new, source)
>
> (assuming of course that target is just a plain string with no regex
> specialness). If you're just cracking a peanut, you probably don't need
> the 30 lb sledgehammer of regular expressions.

Of course, but is the regex library really not smart enough to
special-case and optimize vanilla string substitutions?

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list