【问题标题】:Regex result outputs single match for multiple matches正则表达式结果为多个匹配输出单个匹配
【发布时间】:2019-06-19 13:23:58
【问题描述】:

我在“错误消息”字段中有一个包含多个换行符的文件。 我需要将换行符替换为空格,以便将其加载为单行。

但我无法捕获多个匹配项。 我的正则表达式为整个文件输出单个匹配项。 我需要帮助来创建正则表达式,这将为我提供的示例提供 3 个匹配项。

https://regex101.com/r/DoED69/1

使用正则表达式

^breakmantis.*;(Error;[\s\S]*;Regular;)[\s\S]*breakmantis

**带有换行符的当前文件:**

name;brand;environment;center;Error
breakmantis;Kordel;Production;Bleep Eyeglow;Error;"The message did not pass the validation: 
Error Message='PriceTag' is mandatory, under field Price.
Error Message='PriceAmount' is mandatory, under field Price.
Error Message='BaseQuantity' is mandatory, under field Price.";Regular;;;;24 Apr 2019 14:34 CEST
breakmantis;Kordel;Production;Bleep Eyeglow;Error;"The message did not pass the validation: 
Error Message='PriceTag' is mandatory, under field Price.
Error Message='PriceAmount' is mandatory, under field Price.
Error Message='BaseQuantity' is mandatory, under field Price.";Regular;;;;24 Apr 2019 14:35 CEST
breakmantis;Kordel;Production;Bleep Eyeglow;Error;"The message did not pass the validation: 
Error Message='PriceTag' is mandatory, under field Price.
Error Message='PriceAmount' is mandatory, under field Price.
Error Message='BaseQuantity' is mandatory, under field Price.";Regular;;;;24 Apr 2019 14:36 CEST

预期结果:

breakmantis;Kordel;Production;Bleep Eyeglow;Error;"The message did not pass the validation: Error Message='PriceTag' is mandatory, under field Price.Error Message='PriceAmount' is mandatory, under field Price.Error Message='BaseQuantity' is mandatory, under field Price.";Regular;;;;24 Apr 2019 14:34 CEST
breakmantis;Kordel;Production;Bleep Eyeglow;Error;"The message did not pass the validation: Error Message='PriceTag' is mandatory, under field Price.Error Message='PriceAmount' is mandatory, under field Price.Error Message='BaseQuantity' is mandatory, under field Price.";Regular;;;;24 Apr 2019 14:35 CEST
breakmantis;Kordel;Production;Bleep Eyeglow;Error;"The message did not pass the validation: Error Message='PriceTag' is mandatory, under field Price.Error Message='PriceAmount' is mandatory, under field Price.Error Message='BaseQuantity' is mandatory, under field Price.";Regular;;;;24 Apr 2019 14:36 CEST

【问题讨论】:

    标签: regex


    【解决方案1】:

    怎么样?

    ^breakmantis.*;(Error;[\s\S]*?)CEST$

    正则表达式: https://regex101.com/r/DoED69/2

    【讨论】:

    • 感谢您的快速回答。诀窍来了“*?”返回多个匹配项的量词。我没想过使用“?”匹配器最初。这对我有帮助。
    【解决方案2】:

    尝试匹配:

    \n(错误消息=)

    并替换为

    \1

    在替换中添加前导空格以满足您的需要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多