开发者

C# RegEx Replacement

开发者 https://www.devze.com 2023-02-17 01:23 出处:网络
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.

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 w开发者_JAVA技巧ord "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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号