我需要基于复杂的定界符分割字符串 [英] I need to Split a string based on a complex delimiter

查看:99
本文介绍了我需要基于复杂的定界符分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,我需要根据特定的字符序列(即 nnnn-nn-nn nn:nn:nn INFO)将字符串(log4j日志文件)拆分为数组元素。我目前正在用换行符拆分该日志文件,这很好,除非日志语句本身包含换行符。

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.

似乎我应该能够使用比较器或正则表达式来标识字符串,但是String.Split不能有这样的选择。

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?

推荐答案

使用 Regex.Split()

此正则表达式应该可以使用,但是您可能会找到一个更好的正则表达式:

This regex should work but you might find a better one:

@"\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d INFO"

这篇关于我需要基于复杂的定界符分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆