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.8, Visual Studio 2022, Windows 10
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");

Posted: March 19, 2023

Return to the snippets listing