正则表达式匹配特定单词并忽略特定文本

RegEx to match specific words and ignore specific text

我有以下错误消息列表

def errorMessages = ["Line : 1 Invoice does not foot Reported"
                     "Line : 2 Could not parse INVOICE_DATE value"
                     "Line 3 : Could not parse ADJUSTMENT_AMOUNT value"
                     "Line 4 : MATH ERROR"
                     "cl_id is a required field"
                     "File Error : The file does not contain delimiters"
                     "lf_name is a required field"]

我正在尝试创建一个与 regex "^Line\s(?:(\d+)\s)?\s*:\s+(\d+)?.+" 不匹配但包含文本 Invoice does not foot Reported

的新列表

我想要的新列表应该如下所示

def headErrors= ["Line : 1 Invoice does not foot Reported"
                 "cl_id is a required field"
                 "File Error : The file does not contain delimiters"
                 "lf_name is a required field"]

这就是我现在正在做的事情

regex = "^Line\s(?:(\d+)\s)?\s*:\s+(\d+)?.+"
errorMessages.each{
    if(it.contains('Invoice does not foot Reported'))
        headErrors.add(it)
    else if(!it.matches(regex)
        headErrors.add(it)
}

有没有一种方法可以只使用正则表达式而不是 if else 来完成?

  1. 首先匹配消息部分包含文字Invoice does not foot Reported的行。

  2. 然后在开始时使用否定先行断言,以不匹配以 Line\s(?:(\d+)\s)?\s*:\s+(\d+)? 模式实际匹配的字符开头的行。

正则表达式:

"^Line\s(?:(\d+)\s)?\s*:\s+(\d+)?.*?Invoice does not foot Reported.*|^(?!Line\s(?:(\d+)\s)?\s*:\s+(\d+)?.*).+"

DEMO