How to escape strings for re.finditer?

Cameron Simpson cs at cskk.id.au
Tue Feb 28 16:58:59 EST 2023


On 28Feb2023 18:57, Jen Kris <jenkris at tutanota.com> wrote:
>One question:  several people have made suggestions other than regex 
>(not your terser example with regex you shown below).  Is there a 
>reason why regex is not preferred to, for example, a list comp?  

These are different things; I'm not sure a comparison is meaningful.

>Performance?  Reliability? 

Regexps are:
- cryptic and error prone (you can make them more readable, but the 
   notation is deliberately both terse and powerful, which means that 
   small changes can have large effects in behaviour); the "error prone" 
   part does not mean that a regexp is unreliable, but that writing one 
   which is _correct_ for your task can be difficult, and also difficult 
   to debug
- have a compile step, which slows things down
- can be slower to execute as well, as a regexp does a bunch of 
   housekeeping for you

The more complex the tool the more... indirection between your solution 
using that tool and the smallest thing which needs to be done, and often 
the slower the solution. This isn't absolute;  there are times for the 
complex tool.

Common opinion here is often that if you're doing simple fixed-string 
things such as your task, which was finding instances of a fixed string, 
just use the existing str methods. You'll end up writing what you need 
directly and overtly.

I've a personal maxim that one should use the "smallest" tool which 
succinctly solves the problem. I usually use it to choose a programming 
language (eg sed vs awk vs shell vs python in loose order of problem 
difficulty), but it applies also to choosing tools within a language.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list