正则表达式匹配特定的单词并忽略特定的文本 [英] RegEx to match specific words and ignore specific text

查看:664
本文介绍了正则表达式匹配特定的单词并忽略特定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面的错误消息列表

  def errorMessages = [Line:1 Invoice does not foot Reported
Line:2无法解析INVOICE_DATE值
Line 3:无法解析ADJUSTMENT_AMOUNT值
Line 4:MATH ERROR
cl_id是必填字段
文件错误:文件不包含分隔符
lf_name是必填字段]

我试图创建一个不匹配正则表达式的新列表^ Line \\s(?:( \\d +)\\s)?\ \s *:\\s +(\\d +)?。+但文字为发票没有脚报告



我想要的新列表应该如下

  def headErrors = [Line:1 Invoice does not foot Reported
cl_id是必填字段
文件错误:文件不包含分隔符
lf_name是必填字段]



<

  regex =^ Line \\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)
}

有没有一种方法可以使用正则表达式而不是其他方式?

解决方案


  1. 首先匹配包含文本发票不足的行Reported

  2. 然后在开始时使用负向前视断言,以便不匹配行,前提是字符开头它们实际上由 Line \\s(?:( \\d +)\\s )?\\s *:\\s +(\\d +)? pattern。


正则表达式:

 ^ Line \\s(?:( \\ d +)\\s)?\\s *:\\s+(\\d +)?。*?发票不足报告。* | ^(?! Line \\s (?:( \\d +)\\s)?\\\s *:\\s+(\\d +)?。*)。+

DEMO


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

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.

  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+)? pattern.

Regex:

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

DEMO

这篇关于正则表达式匹配特定的单词并忽略特定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆