[docs] Inadequate documentation on re.sub()

Malik Rumi malik.a.rumi at gmail.com
Tue Sep 3 13:57:15 EDT 2019


What I didn't understand was:

1. That \d was not 'recognized' in Python.
It is so common in regex I never thought otherwise. But Python only has \n,
\t, and one or two others, maybe, and that's it.

2. How to code my way around this issue.
If it were not for finding the language in that bug report, as I already
said, I would never have been able to solve the problem. That's why I
suggested the new clarifying language.


*“None of you has faith until he loves for his brother or his neighbor what
he loves for himself.”*


On Mon, Sep 2, 2019 at 4:50 AM Julien Palard <julien at palard.fr> wrote:

> Hi Malik!
>
> I think you're mixing two concepts:
> - re.sub DO interpret backslash sequences
> - Python strings DO interpret backslash sequences
>
> And both are partially doing the same interpretation:
>
> >>> re.sub(" ", "\n", " ")
> '\n'
> >>> re.sub(" ", r"\n", " ")
> '\n'
>
> In the first example, "\n" is interpreted by the string literal, before
> the function call, a single character (newline) is passed to the function.
> In the second example,  "\n" is not interpreted by the string (due to the
> r (row) prefix), so two characters are given to the function, which
> interprets them as a newline.
>
> So in the sentence: "if it is a string, any backslash escapes in it are
> processed", the doc speak about the sub function interpreting the sequence,
> not the sequence being interpreted by the string.
>
> If I got your misunderstanding right, maybe the following wording would
> help:
>
> > if it is a string, any backslash escapes in it are processed by the
> ``sub`` function.
>
> does it?
> --
> Julien Palard
> https://mdk.fr
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20190903/53015562/attachment.html>


More information about the docs mailing list