Home > other >  C# how to separate a string by numbering (1. 2. ...)
C# how to separate a string by numbering (1. 2. ...)

Time:01-13

I have this string "1: This 2: Is 3: A 4: Test" and would like to split it based on the numbering, like this:

"1: This"
"2: Is"
"3: A"
"4: Test"

I think this should be possible with a regular expression, but unfortunately I don't understand much about it.

This: string[] result = Regex.Split(input, @"\D "); just splits the numbers without the colon and the content behind it.

CodePudding user response:

If you use a capture group () like this:

string[] result = Regex.Split(str, @"(\d :)");

the captured values will be added to the array too. Then all that is left to do is to merge every first value with every second value (we skip index 0 as it is empty):

List<string> values = new();
for (int i = 1; i < result.Length; i  = 2)
{
    values.Add(result[i]   result[i   1]); 
}

There are probably cleaner ways to do this, but this works.

CodePudding user response:

You can use

string[] result = Regex.Split(text, @"(?!^)(?=(?<!\d)\d :)")

See this regex demo. Note that the (?<!\d) negative lookbehind is necessary when you have bullet point with two or more digits. Details:

  • (?!^) - not at the start of string
  • (?=(?<!\d)\d :) - the position that is immediately followed with one or more digits (not preceded with any digit) and a : char.
  •  Tags:  
  • Related