简体   繁体   English

Perl正则表达式以匹配模式

[英]Perl Regex to match the pattern

I am using nagios plugin(check_logs) to scan my logs and send alert, its a perl language based. 我正在使用nagios插件(check_logs)扫描我的日志并发送警报,它基于perl语言。 i am trying to search the particular error from my log file but its not matching i dont know what mistake i am doing . 我正在尝试从日志文件中搜索特定的错误,但不匹配,我不知道自己在做什么。

"POST /aimm/mweb/render.ps? HTTP/1.1" 500

The above string i want to match using regex 我想使用正则表达式匹配以上字符串

"POST \/aimm\/mweb\/render.ps? HTTP\/1.1" 500

The above string i have tried to match the pattern but its not working kindly guide me to solve this thread. 上面的字符串我已经尝试匹配该模式,但是它不能正常工作,请指导我解决此线程。

This is the exact nagios tag 这是确切的nagios标签

{
                tag => "rplogcheck",
                type => 'rotating::uniform',
                logfile => "/opt/fundamo/apache/logs/nofile",
                rotation => "access[_\\d{4}\\-\\d{2}\\-\\d{2}]*.log",
                criticalpatterns => [
                '/"POST \/aimm\/mweb\/render\.ps\? HTTP\/1\.1" 500/'
],

options => 'noprotocol,sticky=300'
    }

);

its not showing any error but its not searching the exact pattern also , normal patterns i can match checked in nagios also the syntax seems correct. 它没有显示任何错误,但也没有搜索确切的模式,正常模式我可以匹配在nagios中检查,语法似乎也正确。

still not able to figure what mistake i am doing in the pattern matching. 仍然无法弄清楚我在模式匹配中犯了什么错误。

A question mark ? 问号? is a regex special character for matching repetition 1 or 0 times. 是用于匹配重复 1或0次的正则表达式特殊字符。

You need to escape it: 您需要对其进行转义:

"POST \/aimm\/mweb\/render\.ps\? HTTP\/1\.1" 500

Additionally, a period . 另外,一个时期. is also a regex special character and so should be escaped as well. 也是正则表达式的特殊字符,因此也应转义。

If you're trying to match a literal string in Perl, I would recommend using an alternative delimiter and quotemeta to avoid having to escape anything at all: 如果您尝试在Perl中匹配文字字符串,我建议您使用替代的定界符和quotemeta以避免完全逃避任何操作:

while (<DATA>) {
    print if m{\Q"POST /aimm/mweb/render.ps? HTTP/1.1" 500};
}

__DATA__
"POST /aimm/mweb/render.ps? HTTP/1.1" 500

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

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