MDaruwalla MDaruwalla - 1 month ago 7
C# Question

How to separate numbers from words, chars and any other marks with whitespace in string

I'm trying to separate numbers from words or characters and any other punctuation with whitespace in string wrote them together e.g. string is:

string input = "ok, here is369 and777, and 20k0 10+1.any word.";


and desired output should be:

ok, here is 369 and 777 , and 20 k 0 10 + 1 .any word.


I'm not sure if I'm on right way, but now what I'm trying to do, is to find if string contains numbers and then somehow replace it all with same values but with whitespace between. If it is possible, how can I find all individual numbers (not each digit in number to be clearer), separated or not separated by words or whitespace and attach each found number to value, which can be used for all at once to replace it with same numbers but with spaces on sides. This way it returns only first occurrence of a number in string:

class Program
{
static void Main(string[] args)
{
string input = "here is 369 and 777 and 15 2080 and 579";
string resultString = Regex.Match(input, @"\d+").Value;

Console.WriteLine(resultString);

Console.ReadLine();
}
}


output:

369


but also I'm not sure if I can get all different found number for single replacement value for each. Would be good to find out in which direction to go

Answer

If what we need is basically to add spaces around numbers, try this:

string tmp = Regex.Replace(input, @"(?<a>[0-9])(?<b>[^0-9\s])", @"${a} ${b}");
string res = Regex.Replace(tmp,   @"(?<a>[^0-9\s])(?<b>[0-9])", @"${a} ${b}");

Previous answer assumed that words, numbers and punctuation should be separated:

string input = "here is369 and777, and 20k0";
var matches = Regex.Matches(input, @"([A-Za-z]+|[0-9]+|\p{P})");
foreach (Match match in matches)
    Console.WriteLine("{0}", match.Groups[1].Value);

To construct the required result string in a short way:

string res = string.Join(" ", matches.Cast<Match>().Select(m => m.Groups[1].Value));