[Python] Python Regex get Unique Multiline Matches

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

  1. Stack

    Stack Membro Participativo

    Since the background would be far to complicated to explain, I am writing Pseudocode, I am only interested in the Python-Regex-Pattern, I hope one of you can help me

    I have the folloing input text (lots of lines with \n as line seperator condensed to '.'):

    .
    .
    1 Order
    order1 stuff
    order1 stuff
    etc
    ShippingMethod: Truck
    .
    .
    2 Order
    order2 stuff
    order2 stuff
    etc
    ShippingMethod: Truck
    .
    .
    Order Summary
    .
    .


    I only want to match the texts in between 'Order' and 'Truck' for each order indiviually, I would then iterate over the results further along in the program.

    my Regex: ( i am splitting into "start, content, end" for better readability).

    pattern = \d\s*Order + [.|\s|\S]* + Truck

    When I execute this match, i get one result, beginning at 1 Order and stopping at the second Truck:

    1 Order
    order1 stuff
    order1 stuff
    etc
    ShippingMethod: Truck
    .
    .
    2 Order
    order2 stuff
    order2 stuff
    etc
    ShippingMethod: Truck


    I want (in this case) exactly two matches which only include one order's contents:

    1 Order
    order1 stuff
    order1 stuff
    etc
    ShippingMethod: Truck


    2 Order
    order2 stuff
    order2 stuff
    etc
    ShippingMethod: Truck


    I hope it's clear what I am looking for. Any help is greatly appreciated.
    Thanks in advance, stay safe, stay healthy!

    Things you might suggest:

    • You have to assume varying amounts of Whitespaces at the start of a line and in between words, since the input text is the result of a PDF-text-extractor. But \n can be trusted. Basically insterad of writing \n write \s*\n
    • I can't use "Order" as the end-part of the pattern, since after the last order the next thing is a summary.
    • "ShippingMethod" is different in my language, that's why I used "Truck" for this example here. I will manage to rewrite.

    Continue reading...

Compartilhe esta Página