Replace Duplicate Words in Text

Description: This little function will replace duplicate words in a line or across multiple lines of text. For instance, if the text reads "What is the the color of the sky?" it will replace "the the" with "the". It will replace them even if the word at the end of a line is duplicated at the beginning of the next line. Uses the System.Text.RegularExpressions namespace.
Tested Platform: .NET 4.5, Visual Studio 2012, Windows 7
Language: C#
// Replaces duplicate words that are side by side with a single copy.

private static String ReplaceDuplicateWords(String inputText)  {
    Regex pattern = new Regex(@"b(w+)(s*$s*|s+)1b", RegexOptions.Multiline | RegexOptions.IgnoreCase);
    return pattern.Replace(inputText, @"$1");
}

Submitted: March 20, 2013

Return to the snippets listing