Use Python's string.replace vs re.sub

Question:

For Python 2.5, 2.6, should I be using string.replace or re.sub for basic text replacements?

In PHP, this was explicitly stated but I can’t find a similar note for Python.

Asked By: wag2639

||

Answers:

As long as you can make do with str.replace(), you should use it. It avoids all the pitfalls of regular expressions (like escaping), and is generally faster.

Answered By: Sven Marnach

Another thing to consider is that if you’re doing rather complex replacements, str.translate() might be what you’re looking for.

Answered By: jathanism

str.replace() should be used whenever it’s possible to. It’s more explicit, simpler, and faster.

In [1]: import re

In [2]: text = """For python 2.5, 2.6, should I be using string.replace or re.sub for basic text replacements.
In PHP, this was explicitly stated but I can't find a similar note for python.
"""

In [3]: timeit text.replace('e', 'X')
1000000 loops, best of 3: 735 ns per loop

In [4]: timeit re.sub('e', 'X', text)
100000 loops, best of 3: 5.52 us per loop
Answered By: chmullig

String manipulation is usually preferable to regex when you can figure out how to adapt it. Regex is incredibly powerful, but it’s usually slower, and usually harder to write, debug, and maintain.

That being said, notice the amount of "usually" in the above paragraph! It’s possible (and I’ve seen it done) to write a zillion lines of string manipulation for something you could’ve done with a 20-character regex. It’s also possible to waste valuable time using "efficient" string functions on tasks a good regex engine could do almost as fast. Then there’s maintainability: Regex can be horribly complex, but sometimes a regex will be simpler and easier to read than a giant block of procedural code.

Regex is fantastic for its intended purpose: searching for highly-variable needles in highly-variable haystacks. Think of it as a precision torque wrench: It’s the perfect tool for a specific set of jobs, but it makes a lousy hammer.

Some guidelines you should follow when you aren’t sure what to use:

If the answer to any of these questions is "yes", you probably want string manipulation. Otherwise, consider regex.

Answered By: Justin Morgan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.