Jasmin Solanki Jasmin Solanki - 6 days ago 6
C# Question

Split string without remove seperator and also keep seperator at start position?

Here my string.

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."


this is my string content and i want to split my string using following separator
[INFO ]
,
[ERROR ]
,
[DEBUG ]
but I don't want to remove that word I used regex positive look back but they append separator
enter code here
at last
i want separator at original position:

I want splited string like this

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."

Answer

Instead of splitting, I suggest matching with a help of regular expressions:

  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

Test:

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

Outcome:

 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.
Comments