我需要根据复杂的分隔符拆分字符串
在 C# 中,我需要根据特定的字符序列(即“nnnn-nn-nn nn:nn:nn INFO”)将字符串(log4j 日志文件)拆分为数组元素。我目前正在按换行符拆分此日志文件,这很好,除非日志语句本身包含换行符。
我不控制输入(日志文件),因此无法以某种方式转义它们。
似乎我应该能够使用比较器或正则表达式来识别字符串,但 String.Split 没有这样的选项。
我是否坚持自己滚动,或者是否有模式或框架组件可以提供帮助?
In C# I need to split a string (a log4j log file) into array elements based on a particular sequence of characters, namely "nnnn-nn-nn nn:nn:nn INFO". I'm currently splitting this log file up by newlines, which is fine except when the log statements themselves contain newlines.
I don't control the input (the log file) so escaping them somehow is not an option.
It seems like I should be able to use a comparator or a regex to identify the strings, but String.Split does not have an option like that.
Am I stuck rolling my own, or is there a pattern or framework component that can be of help here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Regex.Split()为了这。
这个正则表达式应该可以工作,但你可能会找到更好的正则表达式:
Use Regex.Split() for this.
This regex should work but you might find a better one:
我最终不得不在某种程度上自行解决这个问题,因为我需要 Regex.Split 所使用的分隔符。
I ended up having to roll my own to some extent on this one, because I need the delimiter, which Regex.Split eats.