简体   繁体   English

单个RegEx来解析这种日志格式?

[英]Single RegEx to parse this log format?

I am trying to wrap my head around the feasability of parsing a log file with a single RegEx in .NET 我试图围绕用.NET中的单个RegEx解析日志文件的可行性

What is making it difficult is the log file has items that can (but don't always) span multiple lines and that each log file may actually contain multiple 'logs'. 令人困难的是日志文件中的项目可以(但不总是)跨越多行,并且每个日志文件实际上可能包含多个“日志”。 Example format: 示例格式:

log:  
  event 1  
  event 2  
    additional information  
  event 3  
log:  
  event 1  
    additional information  
    more additional information  
  event 2  
    additional information

The necessity here being able to distinguish which events belong to which log and have the additonal information also captured. 这里必须能够区分哪些事件属于哪个日志并且还具有附加信息。 I was able to, of course, just grab events... I have been unable to grab events with additional information, let alone grouping them in to captures by log: 当然,我能够抓住事件...我无法通过其他信息获取事件,更不用说将它们分组到日志中来捕获:

I would appreciate information rather than being handed a solution, so I can learn. 我会很感激信息,而不是提供解决方案,所以我可以学习。 I guess my question is: should this be possible? 我想我的问题是:这有可能吗? It's already been done with a parser I was just trying to discover alternative methods. 它已经完成了解析器我只是想尝试发现替代方法。

This seems like it would be easier and more transparent to parse manually vs trying to do it in a RegEx. 这看起来手动解析比在RegEx中尝试更容易和更透明。 The pattern is pretty simple. 模式非常简单。

Why are you trying to use a single regex for this? 为什么要尝试使用单个正则表达式? Use a proper parser. 使用适当的解析器。

Regular expressions are awesome for simple string manipulation, but once you get to more complex stuff an actual parser is much better. 正则表达式对于简单的字符串操作非常棒,但是一旦你得到更复杂的东西,实际的解析器就会好得多。

It would be possible (and quite easy) to pull out each log entry separately using a pattern, but not to split the match into groups of information using captures in that same pattern. 使用模式单独提取每个日志条目是可能的(并且非常容易),但不能使用相同模式中的捕获将匹配分成信息组。

What you'd need to do is construct a pattern for an info line (basically, space followed by something else to the end of the line), and repeat it. 您需要做的是为信息行构建一个模式(基本上,空格后跟其他东西到行尾),并重复它。

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

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