2016-11-29 25 views
1

在这里我的字符串。不拆下分离器并将分离器保持在起始位置?

string content =  
    @"[INFO ] | 2016-11-28 10:56:19.68 | level to ""Info"" 
    [INFO ] | 2016-11-28 10:56:56.93 | to ""Info"" 
    [DEBUG ] | 2016-11-28 10:56:56.93 | been initialized successfully. 
    [INFO ] | 2016-11-28 11:01:14.05 | to ""Info"" 
    [ERROR] | 2016-11-28 11:01:14.05 | initialized successfully." 

这是我的字符串的内容,我想用下面的分离[INFO ][ERROR ][DEBUG ]分裂我的字符串,但我不希望删除的话,我用正则表达式正回头看,但他们在最后追加分离enter code here 我想在原来的位置分离:

我想splited字符串这样

 1=>[INFO ] | 2016-11-28 10:56:19.68 | level to "Info" 
     2=>[INFO ] | 2016-11-28 10:56:56.93 | to "Info" 
     3=>[DEBUG ] | 2016-11-28 10:56:56.93 | been initialized successfully. 
     4=>[INFO ] | 2016-11-28 11:01:14.05 | to "Info" 
     5=>[ERROR] | 2016-11-28 11:01:14.05 | initialized successfully." 
+2

* “我在这里的字符串” *你如何看待张贴编译'string'?由于'string content ='暗示它在C# – Jim

回答

1

我无耻地开始@DmitryBychenko答案,并试图改善它。

如果你想支持多条目和准确的分离"[INFO ]""[DEBUG ]""[ERROR ]",您可以使用下面的正则表达式的更精确的匹配:

var pattern = @"(\[INFO \]|\[DEBUG \]|\[ERROR \]).+?(?=\[INFO \]|\[DEBUG \]|\[ERROR \]|\z)"; 

var matches = System.Text.RegularExpressions.Regex.Matches(content, pattern, RegexOptions.Singleline) 
    .OfType<Match>() 
    .Select((match, index) => index + "=>" + match.Groups[0].Value.Trim()); 

它从指定的分隔符匹配(在"(\[INFO \]|\[DEBUG \]|\[ERROR \])"部分pattern),并继续匹配,直到达到下一个分隔符(这是".+?(?=\[INFO \]|\[DEBUG \]|\[ERROR \]|\z)"部分)。

这种转变

@"[INFO ] | 2016-11-28 10:56:19.68 | level to ""Info"" 
[INFO ] | 2016-11-28 10:56:56.93 | to ""Info"" 
[DEBUG ] | 2016-11-28 10:56:56.93 | been initialized successfully. 
[INFO ] | 2016-11-28 11:01:14.05 | to ""Info"" 
More info in second line 
[IRRELEVANT TAG] | Noone knows what this is | ""Whatever"" 
[ERROR ] | 2016-11-28 11:01:14.05 | initialized successfully." 

0=>[INFO ] | 2016-11-28 10:56:19.68 | level to "Info" 
1=>[INFO ] | 2016-11-28 10:56:56.93 | to "Info" 
2=>[DEBUG ] | 2016-11-28 10:56:56.93 | been initialized successfully. 
3=>[INFO ] | 2016-11-28 11:01:14.05 | to "Info" 
More info in second line 
[IRRELEVANT TAG] | Noone knows what this is | "Whatever" 
4=>[ERROR ] | 2016-11-28 11:01:14.05 | initialized successfully. 
1

而不是分裂,我建议匹配正则表达式帮助:

string content = 
    @"[INFO ] | 2016-11-28 10:56:19.68 | level to ""Info"" 
     [INFO ] | 2016 - 11 - 28 10:56:56.93 | to ""Info"" 
     [DEBUG ] | 2016 - 11 - 28 10:56:56.93 | been initialized successfully. 
     [INFO ] | 2016-11-28 11:01:14.05 | to ""Info"" 
     [ERROR] | 2016-11-28 11:01:14.05 | initialized successfully."; 

    // square brackets [] 
    // with uppercase text or spaces within it 
    // followed by any characters 
    // up to the end of line or end of the entire text 
    string pattern = @"(\[[A-Z ]+\].+?)(?:\z|\n|\r)"; 

    var result = Regex 
    .Matches(content, pattern, RegexOptions.Multiline) 
    .OfType<Match>() 
    // .Select(match => match.Groups[1].Value}) // if you want just a match 
    .Select((match, index) => $"{index + 1}=>{match.Groups[1].Value}"); 
    // .ToArray(); // <- you may want to materialize the result into, say, an array 

测试:

Console.Write(string.Join(Environment.NewLine, result)); 

结果:

1=>[INFO ] | 2016-11-28 10:56:19.68 | level to "Info" 
2=>[INFO ] | 2016 - 11 - 28 10:56:56.93 | to "Info" 
3=>[DEBUG ] | 2016 - 11 - 28 10:56:56.93 | been initialized successfully. 
4=>[INFO ] | 2016-11-28 11:01:14.05 | to "Info" 
5=>[ERROR] | 2016-11-28 11:01:14.05 | initialized successfully.