我有以下错误消息列表
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?
答案 0 :(得分:2)
首先匹配消息部分中包含文本Invoice does not foot Reported
的行。
然后在开头使用否定前瞻断言,如果它以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+)?.*).+"