C#: Filter dirty words

Posted: September 6, 2013 in .Net/C#
Tags: , ,

The basic design is that system will have a database table to store all the dirty words and when IIS starts, it will load the list to an application variable.

The filter will keep the first and last letter of the dirty word. For example, the f word will become f****k. I didn’t preserve the text length.

/*
retrieve all dirty words, if not in application variable, populate it.
*/
public static List<string> retrieveDirtyWords()
{
     if (HttpContext.Current.Application["Dirty_Words"] == null)
     {
         HttpContext.Current.Application["Dirty_Words"] = your_function_to_retrieve_dirty_words();
     }

     return HttpContext.Current.Application["Dirty_Words"] as List<string>;
}

/*
replace those dirty words
*/
public static string cleanText(string text)
{
     string pattern = string.Format(@"\b(?<dirty>{0})\b", string.Join<string>("|", retrieveDirtyWords()));

     return System.Text.RegularExpressions.Regex.Replace(text, pattern, new System.Text.RegularExpressions.MatchEvaluator(WordScrambler), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
}

/*
keep the first and last character for each matched dirty words.
*/
public static string WordScrambler(System.Text.RegularExpressions.Match match)
{
      System.Text.RegularExpressions.Group g = match.Groups["dirty"];
      if (g != null)
      {
          return g.Value.Substring(0, 1) + "****" + g.Value.Substring(g.Value.Length - 1);
      }

      return string.Empty;
}

For regular expression named capturing group syntax, you can refer to http://www.regular-expressions.info/named.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s