Correct syntax for pathological re.search()

MRAB python at mrabarnett.plus.com
Tue Oct 8 15:11:40 EDT 2024


On 2024-10-07 14:35, Michael F. Stemper via Python-list wrote:
> I'm trying to discard lines that include the string "\sout{" (which is TeX, for
> those who are curious. I have tried:
>     if not re.search("\sout{", line):
>     if not re.search("\sout\{", line):
>     if not re.search("\\sout{", line):
>     if not re.search("\\sout\{", line):
> 
> But the lines with that string keep coming through. What is the right syntax to
> properly escape the backslash and the left curly bracket?
> 
String literals use backslash is an escape character, so it needs to be 
escaped, or you need to use a "raw" string.

However, regex also uses backslash as an escape character.

That means that a literal backslash in a regex that's in a plain string 
literal needs to be doubly-escaped, once for the string literal and 
again for the regex.


More information about the Python-list mailing list