To do this only with RegEx and without further processing, we can reuse Tim Pitzker's response, but passing two consecutive RegEx
We can convey the original from Tim Pitzker’s answer and the same with lookbehind, which will force the regular expression to start recording with the second word.
If you combine the results from two RegEx, you will have all the pairs from the text.
Regex regexObj1 = new Regex( @"(
etc.
For groups of three:
You will need to add the third RegEx to the program:
Regex regexObj3 = new Regex( @"(?<= # Assert that there preceds and will not be captured \w+\s+\w+\s+ # the first and second word followed by any space ) ( # Match and capture in backreference no. 1: \w+ # one or more alphanumeric characters \s+ # one or more whitespace characters. ) # End of capturing group 1. (?= # Assert that there follows... (\w+) # another word; capture that into backref 2. ) # End of lookahead.", RegexOptions.IgnorePatternWhitespace); Match matchResult1 = regexObj1.Match(subjectString); Match matchResult2 = regexObj2.Match(subjectString); Match matchResult3 = regexObj3.Match(subjectString);
source share