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 complete, you can append the b expression to either side of the regex. This denotes a word boundary and, therefore, will only match whole words:
        string pattern = ""; 
foreach (string badWord in lstBad)
pattern += pattern.Length == 0 ? $"{badWord}"
: $"|{badWord}";

pattern = $@"b({pattern})b";
  1. Finally, we will add the Regex.Replace() method that takes the input and looks for the occurrence of the words defined in the pattern, while ignoring case and replacing the bad words with *****:
        return Regex.Replace(input, pattern, "*****", 
RegexOptions.IgnoreCase);
  1. When you have completed this, your SanitizeInput() method will look like this:
        public string SanitizeInput(string input) 
{
List<string> lstBad = new List<string>(new string[]
{ "BadWord1", "BadWord2", "BadWord3" });
string pattern = "";
foreach (string badWord in lstBad)
pattern += pattern.Length == 0 ? $"{badWord}" : $"|{badWord}";

pattern = $@"b({pattern})b";

return Regex.Replace(input, pattern, "*****",
RegexOptions.IgnoreCase);
}
  1. In the console application, add the following code to call the SanitizeInput() method and run your application (if you have already instantiated an instance of RegExDemo in the previous recipe, you don't need to do it again):
        string textToSanitize = "This is a string that contains a  
badword1, another Badword2 and a third badWord3";
RegExDemo oRecipe = new RegExDemo();
textToSanitize = oRecipe.SanitizeInput(textToSanitize);
WriteLine(textToSanitize);
Read();
  1. When you run your application, you will see the following in the console window:

Let's take a closer look at the regular expression generated.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.35.193