简体   繁体   中英

How to use Regex to split a string AND include whitespace

I can't seem to find (or write) a simple way of splitting the following sentence into words and assigning a word to the whitespace between the letters. (VS 2010, C#, .net4.0).

String text = "This is a test.";

Desired result: 
[0] = This
[1] = " "
[2] = is
[3] = " "
[4] = a
[5] = " "
[6] = test.

The closest I have come is:

  string[] words = Regex.Split(text, @"\s");

but ofcourse, this drops the whitespace.

Suggestions are appreciated. Thanks

Edit: There may be one or more spaces between the words. I would like all spaces between the words to be returned as a "word" itself (with all spaces being placed in that "word"). eg, if 5 spaces between a word would be.

String spaceword = " "; <--This is not showing correctly, there should be a string of 5 spaces.

You can use LINQ to add spaces manually between them:

var parts = text.Split(new[]{ ' ' }, StringSplitOptions.RemoveEmptyEntries);
var result = parts.SelectMany((x,idx) => idx != parts.Length - 1 
                                         ? new[] { x, " " }
                                         : new[] { x }).ToList();

Change your pattern to (\\s+) :

        String text = "This        is a   test.";
        string[] words = Regex.Split(text, @"(\s+)");
        for(int i =0; i < words.Length;i++)
        {
            Console.WriteLine(i.ToString() + "," + words[i].Length.ToString() + " = " + words[i]);
        }

Here's the output:

0,4 = This
1,8 =         
2,2 = is
3,1 =  
4,1 = a
5,3 =    
6,5 = test.

You can try this regex, \\w+|\\s+ which uses or operator |

var arr = Regex.Matches(text, @"\S+|\s+").Cast<Match>()
                                         .Select(i => i.Value)
                                         .ToArray();

It just matches both words and spaces and some LINQ stuff is being used so arr is just a String Array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM