Reputation: 904
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);
Upvotes: 1
Views: 1107
Reputation: 96557
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
.
Upvotes: 2