Remove Duplicate Words in C# using Regular Expression

 

Any application which gets input from users or from any other application has the possibility of containing unnecessary and junk values.There are several other option to remove such duplicate values and here i have implemented using Regular Expressions.

Here is my code:

 

using System;

using System.Text.RegularExpressions;

 

public partial class Csharp : System.Web.UI.Page

{

    ArrayList words;

    protected void Page_Load(object sender, EventArgs e)

    {

        words = new ArrayList();

    }

 

    private void RemoveDupes()

    {

 

        string pattern = @"\w*";

        string input = TextBox1.Text;

        MatchEvaluator myEvaluator = new MatchEvaluator(ReplaceD);

        Regex rgx = new Regex(pattern);

        TextBox1.Text = rgx.Replace(input, myEvaluator);

 

    }

    public string ReplaceD(Match m)

    {

 

       if (words.Contains(m.ToString()))

       {

     

            return "";

        }

        else

        {

            words.Add(m.ToString());

            return m.ToString();

        }

 

    }

   

}

 

 

Call this method with RemoveDupes();.For example, if text is "test sample test", it will be "test sample" as return.

Hope it helps.

 

 

 

6 Comments

Comments have been disabled for this content.