好的,正则表达式向导。我希望能够搜索我的日志文件并找到其中包含“错误”一词的任何会话,然后返回整个会话日志条目。
我知道我可以使用字符串/数组来做到这一点,但我想学习如何使用正则表达式来做到这一点,但这就是问题。如果我决定使用正则表达式来做到这一点,我会吗?有一两个问题 http://www.codinghorror.com/blog/archives/001016.html? ;o)
这是日志:
PS:我正在使用 perl Regex 引擎。
Note:我认为我无法在正则表达式中完成此操作。换句话说,我现在有两个问题。 ;o)我已经尝试了下面的解决方案,但是,由于我通过声明我使用的是 Perl 引擎而混淆了问题,因此许多答案都是 Perl 中的(在我的情况下不能使用)。不过,我确实在下面发布了我的解决方案。
2008.08.27 08:04:21 (Wed)------------Start of Session-----------------
Blat v2.6.2 w/GSS encryption (build : Feb 25 2007 12:06:19)
Sending stdin.txt to [email protected] /cdn-cgi/l/email-protection
Subject: test 1
Login name is [email protected] /cdn-cgi/l/email-protection
The SMTP server does not require AUTH LOGIN.
Are you sure server supports AUTH?
The SMTP server does not like the sender name.
Have you set your mail address correctly?
2008.08.27 08:04:24 (Wed)-------------End of Session------------------
2008.08.27 08:05:56 (Wed)------------Start of Session-----------------
Blat v2.6.2 w/GSS encryption (build : Feb 25 2007 12:06:19)
Error: Wait a bit (possible timeout).
SMTP server error
Error: Not a socket.
Error: Not a socket.
2008.08.27 08:06:26 (Wed)-------------End of Session------------------
2008.08.27 08:07:58 (Wed)------------Start of Session-----------------
Blat v2.6.2 w/GSS encryption (build : Feb 25 2007 12:06:19)
Sending stdin.txt to [email protected] /cdn-cgi/l/email-protection
Subject: Lorem Update 08/27/2008
Login name is [email protected] /cdn-cgi/l/email-protection
2008.08.27 08:07:58 (Wed)-------------End of Session------------------
凯尔的答案可能是最糟糕的,但如果您将所有内容都放在一个字符串中并且想要使用单个正则表达式,这里有一个(经过测试的)解决方案:
(第二次更新:修复了一点,现在比以往更具可读性;-)
my $re = qr{
( # capture in $1
(?:
(?!\n\n). # Any character that's not at a paragraph break
)* # repeated
error
(?:
(?!\n\n).
)*
)
}msxi;
while ($s =~ m/$re/g){
print "'$1'\n";
}
丑陋,但你要求它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)