Deutsch   English   Français   Italiano  
<backslashes-20241007145600@ram.dialup.fu-berlin.de>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Correct syntax for pathological re.search()
Date: 7 Oct 2024 13:56:51 GMT
Organization: Stefan Ram
Lines: 38
Expires: 1 Jul 2025 11:59:58 GMT
Message-ID: <backslashes-20241007145600@ram.dialup.fu-berlin.de>
References: <ve0o34$1nep4$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 6/jxDRujriLniuIHYBO5Ug/Tdry8fSUzId9mmkj6i8P6Gl
Cancel-Lock: sha1:Jb+YL5wH5SYtMCaN0ea7vUpIiuA= sha256:fBA4cTfCnf/S4CJP6LmsRqred3jDihrZLWirdEIXP7w=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
	Distribution through any means other than regular usenet
	channels is forbidden. It is forbidden to publish this
	article in the Web, to change URIs of this article into links,
        and to transfer the body without this notice, but quotations
        of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
	services to mirror the article in the web. But the article may
	be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Bytes: 2061

"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