1

I'm adding a profanity filter and what I would like to do is replace the word (or partial word) that's replaced with a string of equal length. The equal length part is where I'm having difficulty.

So if the word being replaced is 3 characters long than I want the text it's replaced with to be 3 characters long. I'm not sure how I can substring my replacement string to match the length of the word being replaced.

Here's my test method:

public static string ProfanityFilter(this string text)
{
    string pattern = @"\bword\b|\bword2\b|\banother*";
    Regex regex = new Regex(pattern);
    string replacement = "*%$@^!#@!@$^()!";
    return regex.Replace(text, replacement);
}

So, if the word "another" is replaced, it would be replaced with "*%$@^!#".

If "word" is replaced it would be replaced with "*%$@^"

If "wording" is replaced it would be replaced with "*%$@^ing"

Update:

I ended up finding the solution...

I created a new method:

 public static string Censored(Match match)
        {
            string replacement = "*%$@^!#@!@$^()!";
            return replacement.Substring(0, match.Captures[0].Length);
        }

Then changed

return regex.Replace(text, replacement);

to

return regex.Replace(text, Censored);
3
  • Be aware that your new method, using the Substring, will throw an exception if the capture length exceeds the replacement string's length. A safer way would be to build up the replacement string from a set of different characters if you really want it to consist of all those different characters. Otherwise you can specify one character and build it up with the String constructor as I've shown in my answer. Commented Mar 16, 2011 at 18:44
  • Interesting to see what happens to "assign". Commented Mar 16, 2011 at 18:52
  • I'm aware that it could exceed it and there wasn't a check in there :) I wasn't finished. Commented Mar 17, 2011 at 21:50

1 Answer 1

2

Try this approach:

string input = "foo word bar word2 foobar another";
string pattern = @"\b(?:word|word2|another)\b";
string result = Regex.Replace(input, pattern, m => new String('*', m.Length));
Console.WriteLine(result);

The idea is to use the overloaded Regex.Replace method that accepts a MatchEvaluator delegate. I am providing the MatchEvaluator via a lambda expression and accessing the Match.Length property to determine the length of the matched profanity.

I redid your pattern to have exact matches by placing the \b metacharacter at the start and end of the alternative matches. However, based on your "wording" = "*%$@^ing" example, it seems you want to support partial matches. In that case you should omit the usage of \b.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll give it a try. I found a way as well and updated my question with the method I found. I'll accept yours after I try it out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.