Correct syntax for pathological re.search()

MRAB python at mrabarnett.plus.com
Tue Oct 8 18:10:03 EDT 2024


On 2024-10-08 21:59, Alan Bawden via Python-list wrote:
> Karsten Hilbert <Karsten.Hilbert at gmx.net> writes:
> 
>             Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
>             Type "help", "copyright", "credits" or "license" for more information.
>             >>> tex = '\sout{'
>             >>> tex
>             '\\sout{'
>             >>>
> 
>     Am I missing something ?
> 
> You're missing the warning it generates:
> 
>          > python -E -Wonce
>          Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
>          Type "help", "copyright", "credits" or "license" for more information.
>          >>> tex = '\sout{'
>          <stdin>:1: DeprecationWarning: invalid escape sequence '\s'
>          >>>

You got lucky that \s in invalid. If it had been \t you would've got a 
tab character.

Historically, Python treated invalid escape sequences as literals, but 
it's deprecated now and will become an outright error in the future 
(probably) because it often hides a mistake, such as the aforementioned 
\t being treated as a tab character when the user expected it to be a 
literal backslash followed by letter t. (This can occur within Windows 
file paths written in plain string literals.)


More information about the Python-list mailing list