2

I want to split the text above in three results. Every result starts with the word "Leistung" and can optionally have a second line starting with "Zusatz".

My regex-expression ist currently this:

/Leistung [ ]*[\w\d äöüÄÖÜ]*\n[\w\d äöüÄÖÜ\*]*/gm

But this does not fit exactly.

This ist the text:

Leistung             Armeotraining ET                                                         Anzahl 1
Zusatz               *TextZusatz_Anf1*
Leistung             Atemtherapie 30                                                          Anzahl 2
Leistung             Aktivierungsgruppe                                                       Anzahl 3
Zusatz               *TextZusatz_Anf3*

The result should be:

Leistung             Armeotraining ET                                                         Anzahl 1
Zusatz               *TextZusatz_Anf1*

Leistung             Atemtherapie 30                                                          Anzahl 2

Leistung             Aktivierungsgruppe                                                       Anzahl 3
Zusatz               *TextZusatz_Anf3*

Can anyone help me with the regex-expression? Thank you in advance!

Tobias

Tobias G.
  • 63
  • 7

3 Answers3

1

You could try this:

/Leistung.*(?:\n(?!Leistung).*)?/gm

RegEx Demo

RegEx Explanation:

  • (?:\n(?!Leistung).*)?: only capture the second line if it does not start with Leistung
alex-dl
  • 802
  • 1
  • 5
  • 12
1

You can split at newlines if the word Leistung is ahead:

\n(?=Leistung\b)

See this demo at regex101 - \b is a word boundary

Use \r?\n for crlf or \R if supported (eg PCRE/PHP).

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
-1

This solution worked for me first:

/Leistung.*(?:\n(?!Leistung).*)?/gm

But then the situation chanced because there could be multile lines between the keyword "Leistung". E.g.:

              Neurologische Diagnostik NTC aus MHK - Anforderung
Leistung             xDoppler-/Duplex-Sonographie der extracraniellen     Anzahl 1
                     hirnversorgenden Arterien
                     hier geht es weiter
Zusatz               Zusatz für Lst 1
Leistung             xDoppler-/Duplex-Sonographie der transcraniellen     Anzahl 1
                     hirnversorgenden Arterien
Zusatz               Zusatz für Lst 2

Auftragsdatum        17.10.2022 13:02

I didn't get a solution for that. Can anyone help?

Tobias G.
  • 63
  • 7
  • How about changing your quantifiers: [`Leistung.*(?:\n(?!Leistung).+)*`](https://regex101.com/r/VD362h/1) (any amount, no empty lines) – bobble bubble Oct 19 '22 at 09:36
  • In https://regex101.com/ `Leistung .*(?:\n(?!Leistung).+)*` it looks good! But in TCL I got the line "Auftragsdatum" at the end. – Tobias G. Oct 19 '22 at 10:32
  • Hmm, that's weird. Are you sure, you've used `.+)*` at the end? Another idea to match at least one non-whitespace in each line, [like this demo](https://regex101.com/r/q9eXpR/1): `Leistung.*(?:\n(?!Leistung)[ \t]*\S.*)*` – bobble bubble Oct 19 '22 at 11:06
  • 1
    Great, the last one works! Thanks! – Tobias G. Oct 19 '22 at 12:22