Regex拆分,可选第二行

  • 本文关键字:二行 拆分 Regex regex
  • 更新时间 :
  • 英文 :


我想把上面的文本分成三个结果。每个结果都以单词"开头;Leisting";并且可以任选地具有以"1"开头的第二行;Zusatz";。

我的正则表达式当前为:

/Leistung [ ]*[wd äöüÄÖÜ]*n[wd äöüÄÖÜ*]*/gm

但这并不完全合适。

这是文本:

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

结果应该是:

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

有人能帮我用正则表达式吗?提前谢谢!

Tobias

你可以试试这个:

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

RegEx演示

RegEx解释:

  • (?:n(?!Leistung).*)?:如果第二行不以Leisting开头,则只捕获第二行

如果单词Leistung在前面,则可以在换行处拆分:

n(?=Leistungb)

在regex101上查看此演示-b是一个单词边界

r?n用于crlfR(如果支持((例如PCRE/PHP(。

这个解决方案首先对我有效:

/Lesting.*(?:\n(?!Leisting(.*(/gm

但随后情况发生了,因为关键字";Leistung";。例如:

神经诊断NTC aus MHK-AnforderungLeisting x Doppler-/Anzahl 1型颅外超声心动图动脉hier geht es weiterZusatz Zusatz für Lst 1Leisting x Doppler-/Transcaniellen Anzahl 1型双联超声心动图动脉Zusatz Zusatz für Lst 2Auftragsdatum 17.10.2022 13:02

我没有得到解决方案。有人能帮忙吗?

最新更新