简体   繁体   English

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

[英]RegEx to match specific words and ignore specific text

i have a list of below error messages 我有以下错误消息列表

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"]

Am trying to create a new list which doesnt match the regex "^Line\\\\s(?:(\\\\d+)\\\\s)?\\\\s*:\\\\s+(\\\\d+)?.+" but has the text Invoice does not foot Reported 正在尝试创建与regex "^Line\\\\s(?:(\\\\d+)\\\\s)?\\\\s*:\\\\s+(\\\\d+)?.+"不匹配的新列表,但具有文字“ Invoice does not foot Reported

my desired new list should be like below 我想要的新列表应如下所示

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"]

this is what am doing for now 这就是现在所做的

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)
}

Is there a way it can be done just using regex instead of if else? 有没有一种方法可以仅使用正则表达式而不是其他方法来完成?

  1. At first match the line which contains the text Invoice does not foot Reported in the message part. 第一次匹配时,包含文本“ Invoice does not foot Reported的行在消息部分Invoice does not foot Reported ”。

  2. Then use a negative lookahead assertion at the start to not to match a line if it is startswith the chars which are actually matched by Line\\\\s(?:(\\\\d+)\\\\s)?\\\\s*:\\\\s+(\\\\d+)? 然后在开头使用否定的超前断言,以使其不匹配(如果该行以与Line\\\\s(?:(\\\\d+)\\\\s)?\\\\s*:\\\\s+(\\\\d+)?实际匹配的字符开头Line\\\\s(?:(\\\\d+)\\\\s)?\\\\s*:\\\\s+(\\\\d+)? pattern. 图案。

Regex: 正则表达式:

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

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM