I need to Split a string based on a complex delimiter

爱⌒轻易说出口 提交于 2019-12-25 06:53:14

问题


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?


回答1:


Use Regex.Split() for this.

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"



回答2:


I ended up having to roll my own to some extent on this one, because I need the delimiter, which Regex.Split eats.

private List<string> splitOnLogDelimiter(string bigString)
{
    Regex r = new Regex("[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2} [0-9]{2,2}:[0-9]{2,2}:[0-9]{2,2} INFO");
    List<string> result = new List<string>();

    //2010-03-26 16:06:38 INFO
    int oldIndex = 0;
    int newIndex = 0;
    foreach (Match m in r.Matches(bigString))
    {
        newIndex = m.NextMatch().Index-1;
        if (newIndex <= 0) break;
        result.Add(bigString.Substring(oldIndex, newIndex - oldIndex));

        oldIndex = newIndex+1;
    }
    return result;


}


来源:https://stackoverflow.com/questions/2526464/i-need-to-split-a-string-based-on-a-complex-delimiter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!