Define a regular expression for repeated words
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main ()
{
Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",RegexOptions.Compiled | RegexOptions.IgnoreCase);
string text = "this is is a test.";
MatchCollection matches = rx.Matches(text);
Console.WriteLine("{0} matches found in:\n {1}", matches.Count, text);
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
Console.WriteLine("'{0}' repeated at positions {1} and {2}",
groups["word"].Value,
groups[0].Index,
groups[1].Index);
}
}
}
Related examples in the same category