[Python-de] regex: Ausdruck matches wen auf einer Zeile, f ails when "freespacing"

robert rottermann <[email protected]> Thu, 8 Dec 2022 08:20:41 +0100
Newsgroups gmane.comp.python.general.german
Message-ID <[email protected]>
Hoi zäme


Ich habe eine regular Expression um Sektionen in einem ini-File zu filtern.

Dieser Ausdruck funktioniert wenn er auf eine Zeile geschrieben wird. Also:

sp0 = '^\[[^\]\r\n]+](?:\r?\n(?:[^\[r\n].*)?)*'

Nicht aber wenn er "free spacing" geschrieben ist:

sp2 = """
^\[[^\]\r\n]+]            # match a section header
(?:                       # followed by the rest of the section
     \r?\n                 # match a line break sequence
     (?:                   # after Each line starts match
         [^[\r\n]           # Any character except "\[" or line break
         .*                # match rest of the line
     )?                    # Group is optional to match empty lines
)*                        # Continue until end of section
"""

import re
FNANME = '/home/robert/workbench/helpers/python/regexp/odoo_samp.cfg'

with open(FNANME) as f:
     txt = f.read()

res0 = re.findall(sp0, txt)    # <----- OK
res2 = re.findall(sp2, txt)    # resultiert in leerer Liste

res0 = re.findall(sp0, txt, re.X)    # re.error: nothing to repeat at position 
16 (line 2, column 8)
res2 = re.findall(sp2, txt, re.X)    # re.error: nothing to repeat at position 
17 (line 3, column 5)


Muss ich noch irgend ein Flag setzen?

danke für Eure Hilfe

schöne Adventszeit

Robert

_______________________________________________
python-de Mailingliste -- [email protected]
Zur Abmeldung von dieser Mailingliste senden Sie eine Nachricht an [email protected]
https://mail.python.org/mailman3/lists/python-de.python.org/
Mitgliedsadresse: [email protected]