简体   繁体   English

php正则表达式匹配日志文件

[英]php regular expression matching log file

I am trying to match exception lines I see in a log file. 我正在尝试匹配在日志文件中看到的异常行。 The log file has the format of "[timestamp] threadid(8 alphanumeric digits) applicationName(any length of alphanumeric characters) E(to denote exception message)" 日志文件的格式为“ [timestamp] threadid(8个字母数字数字)applicationName(任意长度的字母数字字符)E(表示异常消息)”

So a sample log exception message would be: 因此,示例日志异常消息将是:

"[11/27/12 22:33:02:635 EST] 0000009a myApplication E " Anything can come after the E. “ [[11/27/12 22:33:02:635 EST] 0000009a myApplication E”在E之后可以发生任何事情。

I am having trouble finding a regular expression to match this using PHP preg_match 我在使用PHP preg_match查找正则表达式以匹配此表达式时遇到麻烦

Any help would be appreciated. 任何帮助,将不胜感激。

Cheers 干杯

try the following: 尝试以下方法:

'/^\[[^\]]+\]\s\w{8}\s[^\s]+\sE.*/m'

The trailing "m" ensures, that the wedge at the beginning matches the beginning of a line in your log. 尾随的“ m”确保开头的楔形与日志中一行的开头相匹配。

This solution will of course only work, if all the log entries look pretty similar except for the "E". 当然,如果所有日志条目看起来都非常相似(“ E”除外),则该解决方案才有效。 If there are log entries eg without timestamps, it might fail and you have to look for more detailed expressions like sputnick's. 如果有日志条目,例如没有时间戳记,则它可能会失败,并且您必须寻找更详细的表达式,例如sputnick的表达式。

尝试使用此正则表达式(带有捕获组 ):

/^\[\d+/\d+/\d+\s+\d+:\d+:\d+:\d+\s+\w+\]\s+\w{8}\s+\w+\s+E(.*)/

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

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