Correct syntax for pathological re.search()

Michael F. Stemper michael.stemper at gmail.com
Mon Oct 7 10:14:53 EDT 2024


On 07/10/2024 08.56, Stefan Ram wrote:
> "Michael F. Stemper" <michael.stemper at gmail.com> wrote or quoted:
>> if not re.search("\\sout\{", line):
> 
>    So, if you're not down to slap an "r" before your string literals,
>    you're going to end up doubling down on every backslash.

Never heard of that before, but it did the trick.

>    Long story short, those double backslashes in your regex?
>    They'll be quadrupling up in your Python string literal!

> for line in lines:
>      product = re.search( "\\\\sout\\{", line )

This also worked.

For now, I'll use the "r" in a cargo-cult fashion, until I decide which
syntax I prefer. (Is there any reason that one or the other is preferable?)

Thanks for your help,
Mike
-- 
Michael F. Stemper
Economists have correctly predicted seven of the last three recessions.



More information about the Python-list mailing list