You can match these substrings with [^\s;,]+
pattern. Splitting with [\s;,]+
is not recommended as there are often empty strings in the resulting array after splitting (due to either matches at the start/end of string, or consecutive matches).
See the regex demo.
In C#, use
var matches = Regex.Matches(text, @"[^\s;,]+")
.Cast<Match>()
.Select(x => x.Value)
.ToList();
The [^\s;,]+
matches one or more (due to +
quantifier) occurrences of any char other than ([^...]
is a negated character class) a whitespace (\s
), semi-colona dn a comma.
Non-regex approach
You can split your string with comma and semi-colon, remove empty entries and then trim the strings in the resulting array:
var text = "12f3,, 456;;;;\n\n227- , 999";
var res = text.Split(new[] {";", ","}, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim());
Console.WriteLine(string.Join("\n", res));
See the C# demo. Output:
12f3
456
227-
999