【问题标题】:Regex to extract between start and end strings and match the entire line containing the end string正则表达式在开始和结束字符串之间提取并匹配包含结束字符串的整行
【发布时间】:2018-01-06 05:36:35
【问题描述】:

问题

我有一个很长的非结构化文本,我需要从中提取多组文本。

我有一个理想的开始和结束。

这是一个非结构化文本被截断的例子:

more useless gibberish at the begininng...
separated by new lines...
START                                              Fund Class                                            Fund Number                                   Fund Currency
XYZ                                      XYZ                                           XYZ                                          USD

                                                                                                                                                                bunch of text with lots of newlines in between...                                              Closing                              11.11                                                1,111.11   111,111.11

more useless gibberish between the groups...
separated by new lines...

START                                              Fund Class                                            Fund Number                                   Fund Currency
XYZ                                      XYZ                                           XYZ                                          USD

The word START appears in the middle sometimes multiple times, but it's fine                                                                                                                                                             bunch of text with lots of newlines in between...                                              Closing                              22.22                                                2,222.22   222,222.22

more useless gibberish at the end...
separated by new lines...

我尝试过的

在上面的示例中,我想提取出位于STARTClosing 之间的两组文本

我已经使用正则表达式成功地做到了

/(?<=START)(?s)(.*?)(?=Closing)/g

这是https://regex101.com/r/vo7CLx/1/的结果

怎么了?

不幸的是,我还需要提取包含Closing字符串的行尾。

如果您从regex101 链接中注意到,第一场比赛中有一个Closing 11.11 1,111.11 111,111.11。在第二场比赛中还有Closing 22.22 2,222.22 222,222.22

哪个正则表达式不匹配。

有没有办法在单个正则表达式中做到这一点?这样就连带数字的结束标签也包括在内?

【问题讨论】:

标签: regex string pcre


【解决方案1】:

试试这个正则表达式:

(?s)(?<=START)(.*?Closing(?:\s*[\d.,])+)

Click for Demo

说明:

  • (?s) - 单行修饰符,表示正则表达式中的 . 将匹配换行符
  • (?&lt;=START) - 正向向后查找以找到紧跟在 START 之前的位置
  • (.*?Closing(?:\s*[\d.,])+) - 懒惰地匹配 0+ 次出现的任何字符,直到下一次出现单词 Closing,其后跟一个序列 (?:\s*[\d.,])+
    • (?:\s*[\d.,])+ - 匹配 0+ 个空格后跟一个数字或 .,。最后的 + 意味着我们必须匹配这个子模式 1 次或更多次

【讨论】:

  • 谢谢!你的解决方案加上解释太棒了。
  • 我突然好奇了,如果我也想包含 START,我该怎么做?不想回答也没关系。我是出于好奇而问的。这已经有很大帮助了。干杯:)
  • 只需删除正面的lookbehind,如(?s)(START.*?Closing(?:\s*[\d.,])+)所示
  • 啊……我试过(?s)(?&lt;=START.*?Closing(?:\s*[\d.,])+)但没有用。现在我明白为什么了。非常感谢。 :)
【解决方案2】:

(START)(?s)(.*?)(Closing)(\s+((,?\d{1,3})+.\d+))+ 应该匹配您想要的所有内容,see here

【讨论】:

    【解决方案3】:

    你可以试试这个正则表达式,

    START(.*)Closing(.*)(((.?\d{1,3})+.\d+)+.\d+.\d+.\d)\d
    

    【讨论】:

      猜你喜欢
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 2018-03-05
      • 2017-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多