How to get words of line 3 by 3 with overlapping?

Let's say I have a suggestion like this:

A regular expression to output words from a string from a specific position

I need to write a regular expression that, in combination with the for loop, will output the first 3 words from the beginning of the sentence at the beginning of the loop (0).

When the cycle continues, the regular expression will move to the next part of the sentence, the regular expression will skip the first word and take the next 3 words in a line.

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";

etc. to the end of the line.

I figured out how to extract the first word from a string, but it is pretty much I am very new to Regex and I did it like this:

^([\w\-]+)

But that is not what I need.

+4
source share
2 answers

.

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
}
+2

( Split(' ')) ( ):

public static IEnumerable<T[]> SlidingWindow<T>(this IEnumerable<T> source,
                                                int windowSize) {
  if (null == source)
    throw new ArgumentException("source");
  else if (windowSize <= 0)
    throw new ArgumentOutOfRangeException("windowSize", 
      "Window size must be positive value");

  List<T> window = new List<T>(windowSize);

  foreach (var item in source) {
    if (window.Count >= windowSize) {
      yield return window.ToArray();

      window.RemoveAt(0);
    }

    window.Add(item);
  }

  // Or (window.Count >= windowSize) if you don't want partial windows 
  if (window.Count > 0)
    yield return window.ToArray();
}

SlidingWindow , , , - ( ).

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

// Regex solution: get all matches as usual...
var result = Regex
  .Matches(sentence, @"[\w\-]+") // you don't want ^ anchor
  .OfType<Match>()
  .Select(match => match.Value)
  .SlidingWindow(3); // and represent them as sliding windows..

var test = String.Join(Environment.NewLine, result
  .Select(line => $"[{string.Join(" ", line)}]")); 

Console.Write(test);

[Regex for taking]
[for taking out]
[taking out words]
[out words out]
[words out of]
[out of a]
[of a string]
[a string from]
[string, from, a]
[from a specific]
[a specific position]

, , Split, :

// Split solution: as usual + final representation as sliding window 
var result = sentence
  .Split(' ')        // just split...
  .SlidingWindow(3); // ... and represent as sliding windows
+2

Source: https://habr.com/ru/post/1658347/


All Articles