How to do it...

  1. Create a new method in your RegExDemo class called SanitizeInput() and let it accept a string parameter:
        public string SanitizeInput(string input)         {                          }
  1. Add a list of type List<string> to the method that contains the bad words we want to remove from the input:
        List<string> lstBad = new List<string>(new string[]        {  "BadWord1", "BadWord2", "BadWord3" });
In reality, you might make use of a database call to read the blacklisted words from a table in the database. You would usually not hardcode them in a list like this.
  1. Start constructing the regex that we will use to look for the blacklisted words. You concatenate the words with the | (OR) metacharacter so that the regex will match any of the words. When the list is ...

Get C# 7 and .NET Core Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.