[issue17426] \0 in re.sub substitutes to space

Guido van Rossum report at bugs.python.org
Fri Mar 15 17:41:59 CET 2013


Guido van Rossum added the comment:

Anatoly, your question belongs on python-list or stack overflow, not in the
tracker.

--Guido van Rossum (sent from Android phone)
On Mar 15, 2013 9:28 AM, "anatoly techtonik" <report at bugs.python.org> wrote:

>
> anatoly techtonik added the comment:
>
> Matthew, finally the right answer. Thanks!
>
>
> Looking further, there is a bug in processing backslashes in raw literal
> replacement strings. re.sub ignores raw strings as replacements. This can
> be even more confusing for people who look for more advanced equivalent for
> string replace().
>
>
>   patt = "aaa"
>   repl = r"zed \0 org"
>
>   print(" aaa ".replace(patt, repl))
>
>   import re
>   print(re.sub(patt, repl, " aaa "))
>
> This gives:
>
>  zed \0 org
>  zed   org
>
> With `repl = "zed \0 org"`, the output matches:
>
>   zed   org
>   zed   org
>
> ----------
>
> _______________________________________
> Python tracker <report at bugs.python.org>
> <http://bugs.python.org/issue17426>
> _______________________________________
>

----------

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


More information about the Python-bugs-list mailing list