正则表达式排除不包含单词但包含单词的行

时间:2021-04-17 03:06:27

I am using jenkins editable email plugin where i need to use regex to filter logs

我正在使用jenkins可编辑的电子邮件插件,我需要使用正则表达式来过滤日志

I have this regex

我有这个正则表达式

 regex="(.*)fatal:(.*)"

which basically matches line start with fatal: so that i can grab errors

它基本上匹配行开始与致命:所以我可以抓住错误

Example line look like this

示例行看起来像这样

fatal: [localhost]: FAILED! => {"attempts": 1, "changed": true, "cmd": "nslookup test1.local", 

But i want to grab all fatal erros except which conatin few worda like nslookup.

但是我想要抓住所有致命的错误,除了conhold很少像nslookup一样。

I want to ignore the above line which has nslookup and all other should be ok

我想忽略上面有nslookup的行,所有其他的都应该没问题

1 个解决方案

#1


2  

You can do a negative look ahead with a regex. This would work for your example:

你可以用正则表达式做一个负向的展望。这适用于您的示例:

^(.*)fatal: ((?!nsookup).)*$

To check for two different lookahead words:

要检查两个不同的超前单词:

^(.*)fatal: ((?!nslookup)(?!stuff).)*$

The (.*) at the beginning is only required if there can be something (whitespace or other characters) before "fatal:"

只有在“致命”之前可能存在某些内容(空格或其他字符)时,才需要开头的(。*):

You can play with the expression with this link: https://regex101.com/r/ezxA5s/1

您可以使用以下链接播放表达式:https://regex101.com/r/ezxA5s/1

#1


2  

You can do a negative look ahead with a regex. This would work for your example:

你可以用正则表达式做一个负向的展望。这适用于您的示例:

^(.*)fatal: ((?!nsookup).)*$

To check for two different lookahead words:

要检查两个不同的超前单词:

^(.*)fatal: ((?!nslookup)(?!stuff).)*$

The (.*) at the beginning is only required if there can be something (whitespace or other characters) before "fatal:"

只有在“致命”之前可能存在某些内容(空格或其他字符)时,才需要开头的(。*):

You can play with the expression with this link: https://regex101.com/r/ezxA5s/1

您可以使用以下链接播放表达式:https://regex101.com/r/ezxA5s/1