简体   繁体   English

解析日志条目的正则表达式是什么

[英]What is the regular expression for parsing a log entry

This question is different from the "normal" parse new lines in a string because this question uses three different regular expression strings to parse a single line of text... which may or may not have multiple lines 此问题与“正常”分析字符串中的新行不同,因为此问题使用三个不同的正则表达式字符串来分析一行文本...可能有多行也可能没有多行

I have the following log entries and would like to have a regular expression that parses both of them... currently it only parses the first line. 我有以下日志条目,并希望有一个解析它们两个的正则表达式...目前,它仅解析第一行。

Here are the log entries 这是日志条目

NOTE: the second log entry has a hard return ("\\r\\n") in it 注意:第二个日志条目中有一个硬返回(“ \\ r \\ n”)

[2018-05-25 08:23:54.6040][Manager.Calls.Manager][GetManagerID]
[2018-05-25 08:23:54.6040][Manager.Calls.Manager][Status as of 5/25/2018 8:23:54 AM
Expires 1/1/0001 12:00:00 AM]

Here is the regular expression that I am currently using: 这是我当前正在使用的正则表达式:

(\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{4}\])\[(.*?)\]\[(.*?)\]

I think I need something to get the "\\r\\n" line endings... 我想我需要一些东西来获得“ \\ r \\ n”行结尾...

Here is how I am calling Regex 这是我给Regex打电话的方式

match = Regex.Match(logEntryText, pattern, RegexOptions.Compiled | RegexOptions.Multiline);

More info: 更多信息:

I tried this for the last reg expression 我在最后一个reg表达式中尝试过

\[(.*?)(\n|\r|\r\n)\]

but it too fails 但它也失败了

I think that you could benefit from some preprocessing for your data before performing the parsing operation. 我认为您可以在执行解析操作之前从数据预处理中受益。 For example, check if the log line ends with ] , and if it doesn't, append the next line to it; 例如,检查日志行是否以]结尾,如果不是,则将下一行追加到该行之后。 then process the resulting complete log entry. 然后处理生成的完整日志条目。

Edit: I was assuming that you have just a list of lines (eg from a text file). 编辑:我假设您只有一行列表(例如,来自文本文件)。 If what you have instead is a list of log entries which sometimes include the hard return, then just remove it before processing: logEntry.Replace("\\r\\n","") . 如果您拥有的是有时有时包含硬返回的日志条目列表,则只需在处理之前将其删除: logEntry.Replace("\\r\\n","")

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

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