[英]Regular Expression to match outer brackets but ignoring comment lines with brackets
[英]How can I match regular expression with new lines and within brackets?
这是我的文字示例:
[10:24:33.361][RestApi.RestEngine
ERROR GET
process-meter/util/verify
Status code Forbidden
Content {"versions":1}
]
时间部分:
[10:24:33.361]
很简单:
(\[\d{2}:\d{2}:\d{2}\.\d{3}\])
我需要帮助的是正则表达式,以匹配时间后的字符串,该字符串由方括号分隔,并且其中包含新行
因此,我已经编辑了此问题,以将其放回“新行”正则表达式问题...,解决方案是重新编写日志记录对象,以将输出放在一行上,或者按照Mike Robinson的建议,使用pre -parser,然后使用正则表达式。
请将此标记为已关闭。
使用RegexOptions.MultiLine
var match = Regex.Match("myInputStr", "my pattern", RegexOptions.Multiline);
这是有关其功能的文档:
//
// Summary:
// Multiline mode. Changes the meaning of ^ and $ so they match at the beginning
// and end, respectively, of any line, and not just the beginning and end of the
// entire string. For more information, see the "Multiline Mode" section in the
// Regular Expression Options topic.
Multiline = 2,
我相信这可以解决问题:
var pattern = @"(\[\d{2}:\d{2}:\d{2}\.\d{3}\])(\[[\s\S]*\])";
var matches = Regex.Matches(input, pattern);
var group = matches.Count > 0 ? matches[0].Groups[2] : null; // group == null if the input isn't a match for the pattern.
这是group.Value
:
[RestApi.RestEngine
ERROR
GET process-meter/util/verify
Status code Forbidden
Content {"versions":[1]}
]
所有出色的答案,当然,但是...此外...
“当您沿着报春花小径漫步时,”请记住两点重要事项:
在这种情况下,我会退后一步,考虑awk
实用程序采用的一般方法,该方法首先对文件中的各行进行分类 ,然后根据识别出的“行”类型执行某些操作。 即使所讨论的文件似乎包含“更多嵌套” 与 “严格逐行”的结构,也许您这次可以摆脱逐行策略。
要在awk
以外的工具中模拟相同的逻辑,您首先要使用正则表达式来确定“您拥有什么样的源代码行”,然后调用特定于该行代码的子例程。
但是,最后,“有时您实际上需要解析器。”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.