public static string ReplaceBadWords(this string text) { const RegexOptions matchOptions = RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Singleline; if (!Config.FilterBadWords || String.IsNullOrEmpty(text)) { return(text); } List <BadwordInfo> bWords = SnitzCachedLists.GetCachedBadWordList(); string[] ReplaceText = new string[bWords.Count]; string[] BadWords = new string[bWords.Count]; int ii = 0; foreach (BadwordInfo dr in bWords) { string bw = dr.Badword; BadWords[ii] = Regex.Escape(bw); ReplaceText[ii] = dr.Replace; ++ii; } string strBadWords = String.Join("|", BadWords); try { Regex regexObj = new Regex(@"\b" + strBadWords, matchOptions); Match matchObj = regexObj.Match(text); while (matchObj.Success) { int pos = Array.IndexOf(BadWords, Regex.Escape(matchObj.Value)); string rText = ReplaceText[pos]; text = Regex.Replace(text, matchObj.Value, rText, matchOptions); matchObj = matchObj.NextMatch(); } } catch { // Most likely cause is a syntax error in the regular expression // throw (ex); return(text); } return(text); }