[issue2650] re.escape should not escape underscore

James Y Knight report at bugs.python.org
Sat Jan 8 04:25:41 CET 2011


James Y Knight <foom at users.sourceforge.net> added the comment:

I just ran into the impl of escape after being surprised that '/' was being escaped, and then was completely amazed that it wasn't just implemented as a one-line re.subn. Come on, a loop for string replacement? This is *in* the freaking re module for pete's sake!

The extra special \\000 behavior seems entirely superfluous, as well. re works just fine with nul bytes in the pattern; there's no need to special case that.

So:
return  re.subn('([^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890])', '\\\\\\1', pattern)[0]

or, for the new proposed list of special chars:
return re.subn('([][.^$*+?{}\\|()])', '\\\\\\1', pattern)[0]


(pre-compilation of pattern left as an exercise to the reader)

----------
nosy: +foom

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue2650>
_______________________________________


More information about the Python-bugs-list mailing list