[英]Parse multiline log entries using a regex
我正在嘗試使用此正則表達式解析C#應用程序中的日志條目: (^[0-9]{4}(-[0-9]{2}){2}([^|]+\\|){3})(?!\\1)
用於日志格式,如[日期(某種格式)] | [等級] | [appname] | [留言] 。
在哪里(我認為):
^
匹配一行的開頭(在regex101上啟用/ gm ) [0-9]{4}(-[0-9]{2}){2}
之后是2015-03-03的開始日期 ([^|]+\\|){3})
后跟剩余的日期,日志級別和應用程序名稱 (?!\\1)
后面沒有新的日志條目的開頭(應該是消息) 例如,我有以下4個日志條目(由換行符分隔以便澄清):
2015-03-03 19:30:47.2725|INFO|MyApp|This is a single line log message. 2015-03-03 19:31:29.1209|INFO|MyApp|This log message has multiple lines with 2015-03-03 a date in it. 2015-03-03 19:32:50.1106|INFO|MyApp|This log message has multiple lines but just text only. 2015-03-03 19:33:20.2683|ERROR|MyApp|This log message has multiple lines but also some confusing text like 2015-03-03 19:33:20.2683|ERROR| which should still be a valid log message.
但是當我在regex101上測試它時,正則表達式不捕獲消息,可能是因為我不明白如何捕獲負向前瞻。
如果我在正則表達式中包含.*
: (^[0-9]{4}(-[0-9]{2}){2}([^|]+\\|){3}).*(?!\\1)
它匹配消息但只有一行(因為.
與換行符不匹配)。
那么如何捕獲(多行)消息呢?
你可以使用這個正則表達式:
(^\d{4}(-\d{2}){2}([^|]+\|){3})([\s\S]*?)\n*(?=^\d{4}.*?(?:[^|\n]+\|){3}|\z)
這個正則表達式也應該在C#中工作,只需確保使用MULTILINE
標志。
這樣的事情應該有效。
請參閱正則表達式中的注釋。
( mod :為EOS或單行消息選擇換行符)
@"(?m)^[0-9]{4}(?:-[0-9]{2}){2}(?:[^|\r\n]+\|){3}((?:(?!^[0-9]{4}(?:-[0-9]{2}){2}(?:[^|\r\n]+\|){3}).*(?:\r?\n)?)+)"
格式化( 使用此 ):
(?m) # Modifier - multiline
^ # BOL
[0-9]{4} # Message header
(?: - [0-9]{2} ){2}
(?: [^|\r\n]+ \| ){3}
( # (1 start), The Message
(?:
(?! # Assert, not a Message header
^ # BOL
[0-9]{4}
(?: - [0-9]{2} ){2}
(?: [^|\r\n]+ \| ){3}
)
.* # Line is ok, its part of the message
(?: \r? \n )? # Optional line break
)+
) # (1 end)
輸出:
** Grp 0 - ( pos 0 , len 74 )
2015-03-03 19:30:47.2725|INFO|MyApp|This is a single line log message.
** Grp 1 - ( pos 36 , len 38 )
This is a single line log message.
--------------
** Grp 0 - ( pos 74 , len 108 )
2015-03-03 19:31:29.1209|INFO|MyApp|This log message has multiple
lines with
2015-03-03
a date in it.
** Grp 1 - ( pos 110 , len 72 )
This log message has multiple
lines with
2015-03-03
a date in it.
--------------
** Grp 0 - ( pos 182 , len 97 )
2015-03-03 19:32:50.1106|INFO|MyApp|This log message has
multiple lines
but just text only.
** Grp 1 - ( pos 218 , len 61 )
This log message has
multiple lines
but just text only.
--------------
** Grp 0 - ( pos 279 , len 186 )
2015-03-03 19:33:20.2683|ERROR|MyApp|This log message has multiple lines but
also some confusing text like
2015-03-03 19:33:20.2683|ERROR| which should
still be a valid log message.
** Grp 1 - ( pos 316 , len 149 )
This log message has multiple lines but
also some confusing text like
2015-03-03 19:33:20.2683|ERROR| which should
still be a valid log message.
你使用什么正則表達式引擎? 例如,在Java中有一個標志來告訴“。” 匹配換行符。
以下正則表達式似乎可以解決問題:
/(([0-9]{4})(-[0-9]{2}){2}([^|]+\|){3})((.(?!\2))*)/sg
我對您的查詢所做的修改主要是一些清理(您的日期捕獲組錯誤)。 然后我加了一個。 和*在最后的捕獲組中。 https://regex101.com/r/fU1vV1/2
最重要的部分是使用sg標志。 g讓它獲得所有比賽。 s使它像一條線一樣對待它(否則你的負面前瞻永遠不會起作用)。 如果您可以保證評論在一行(它們在您的示例中),那么所有這些都是不必要的,因為您可以捕獲到行的末尾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.