c# - Split on numeric to letters excluding comma



I have a string containing "0,35mA" I now have the code below, which splits "0,35mA" into





List<string> splittedString = new List<string>();
foreach (string strItem in strList)
    splittedString.AddRange(Regex.Matches(strItem, @"\D+|\d+")
        .Select(m => m.Value)

What I want is the code to be splitted into



How do I achieve this?

3 Answers: 

It looks like you want to tokenize the string into numbers and everything else.

A better regex approach is to split with a number matching pattern while wrapping the whole pattern into a capturing group so as to also get the matching parts into the resulting array.

Since you have , as a decimal separator, you may use

var results = Regex.Split(s, @"([-+]?[0-9]*,?[0-9]+(?:[eE][-+]?[0-9]+)?)")
        .Where(x => !string.IsNullOrEmpty(x))

See the regex demo:

enter image description here

The regex is based on the pattern described in Matching Floating Point Numbers with a Regular Expression.

The .Where(x => !string.IsNullOrEmpty(x)) is necessary to get rid of empty items (if any).


I assume that all your strings will have the same format.

So, try using this regex:

 string regex = "([\\d|,]{4})|[\\w]{2}";

It should work.

 var st = "0,35mA";  

 var li = Regex.Matches(st, @"([,\d]+)([a-zA-z]+)").Cast<Match>().ToList();  

 foreach (var t in li)
     Console.WriteLine($"Group 1 {t.Groups[1]}")
     Console.WriteLine($"Group 2 {t.Groups[2]}");

Group 1 0,35

Group 2 mA