Sujet : Re: Correct syntax for pathological re.search()
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 07. Oct 2024, 15:56:51
Autres entêtes
Organisation : Stefan Ram
Message-ID : <backslashes-20241007145600@ram.dialup.fu-berlin.de>
References : 1
"Michael F. Stemper" <
michael.stemper@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.
Long story short, those double backslashes in your regex?
They'll be quadrupling up in your Python string literal!
main.py
import re
lines = r'''
abcdef
\sout{abcdef
abcdef
abc\sout{def
abcdef
abcdef\sout{
abcdef
'''.strip().split( '\n' )
for line in lines:
product = re.search( "\\sout\{", line )
if not product:
print( line )
stdout
abcdef
abcdef
abcdef
abcdef