[Python] Match all lines with a pattern after a text until pattern matching failure regex

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have a text:


    {{Verkleinerungsformen}}
    :[1] [[Äpfelchen]], [[Äpfelein]], [[Äpflein]]

    {{Oberbegriffe}}
    :[1] [[Kernobst]], [[Obst]]; [[Frucht]]
    :[4] [[Kot]]
    :[7] [[Gut]]

    {{Unterbegriffe}}
    :[1] [[Augustapfel]], [[Bohnapfel]], [[Bratapfel]], [[Essapfel]], [[Fallapfel]],




    I'm interested in extracting all items under {{Oberbegriffe}} that have the pattern [[Text]] including all lines until it reach another line that does not have :[NUMBER-HERE] at the begin

    so in the above example it should return an array of these strings:

    Kernobst, Obst, Frucht, Kot, Gut

    what I have tried is:

    re.search(r'{{Oberbegriffe}}\n(?::?\n)?([^\n]+)', text)

    But it matches only the full first line. It's ok if there is a way to extract all lines with the pattern and it retruns this string

    :[1] [[Kernobst]], [[Obst]]; [[Frucht]]
    :[4] [[Kot]]
    :[7] [[Gut]]

    Continue reading...

Compartilhe esta Página