User987 User987 - 1 month ago 4
C# Question

How to get words of a string 3 by 3 with overlap?

Let's say I have a sentence like this:


Regex for taking out words out of a string from a specific position


I need to write a regex that would, combined with a for loop, take out first 3 words from the beginning of the sentence at first (0) loop.

As the loop goes on, the regex would move onto the next part of the sentence, the regex skips the first word and takes the next 3 words in string.

So for example:

1st loop I'd get: "Regex for taking";
2nd loop I'd get: "for taking out";
3rd loop I'd get: "taking out words";


and so on till the end of the string.

I've figured out how to take a first word out of the string, but that's pretty much it, I'm very new to Regex, and I've done it like this:

^([\w\-]+)


But this isn't what I need.

Answer

Here is a non regex solution.

public static IEnumerable<List<string>> StrangeLoop(string source)
{
    // If word separators are anything other than whitespaces 
    // then change parameters for Split
    var words = source.Split(null); 
    for (int i = 0; i < words.Length - 2; i++)
    {
        yield return new List<string>() { words[i], words[i + 1], words[i + 2] };
    }
}

var sentence = "Regex for taking out words out of a string from a specific position";

foreach (var triad in StrangeLoop(sentence))
{
    //use triad
}