Regex.Split () clause for words while maintaining spaces

I use Regex.Split()to enter the user and convert him into individual words in the list, but at the moment when he removes any spaces that they add, I would like him to keep spaces.

string[] newInput = Regex.Split(updatedLine, @"\s+");
+5
source share
2 answers
string text = "This            is some text";
var splits = Regex.Split(text, @"(?=(?<=[^\s])\s+)");

foreach (string item  in splits)
    Console.Write(item);
Console.WriteLine(splits.Count());

This will give you 4 splits, each of which will save all the main spaces.

(?=\s+)

The funds are split from the point where gaps are in front. But if you use this alone, it will create 15 sections for example text, because each space is followed by a different space in case of repeated spaces.

(?=(?<=[^\s])\s+)

, , , .

, , ,

(?=(?<=^|[^\s])\s+)

, .

+6

, "", , , . . :

string updatedLine = "user,input,two words,even three words";
string[] newInput = Regex.Split(updatedLine, @",");

:

string updatedLine = "user, input,   two words,    even three words";
string[] newInput = Regex.Split(updatedLine, @",\s+|,");
0

All Articles