Replace emoticon with word in tweet using regex c#

2019-07-30 16:22发布

问题:

Basically the idea is to map the emoticons in the string to actual words. say for :) you replace it with happy. A more clear example would be. Original: Today is a sunny day :). But tomorrow it is going to rain :(. Final: Today is a sunny day happy. But tomorrow it is going to rain sad.

I have trying a solution using a common regex for all emoticons but I am not sure once you detect it is an emoticon, how to go back and replace each one with appropriate word. I need it only for three emoticons :),:( and :D. Thank you.

回答1:

Use Regex.Replace method that takes a custom match evaluator.

static string ReplaceSmile(Match m) {
    string x = m.ToString();
    if (x.Equals(":)")) {
        return "happy";
    } else if (x.Equals(":(")) {
        return "sad";
    }
    return x;
}

static void Main() {
    string text = "Today is a sunny day :). But tomorrow it is going to rain :(";
    Regex rx = new Regex(@":[()]");
    string result = rx.Replace(text, new MatchEvaluator(ReplaceSmile));
    System.Console.WriteLine("result=[" + result + "]");
}


回答2:

Why don't you use a plain replace? You have just three fixed patterns:

str = str.Replace(":(", "text1")
         .Replace(":)", "text2")
         .Replace(":D", "text3")


回答3:

A more general solution:

var emoticons = new Dictionary<string, string>{ {":)", "happy"}, {":(", "sad"} };
string result = ":) bla :(";
foreach (var emoticon in emoticons)
{
    result = result.Replace(emoticon.Key, emoticon.Value);
}

For any additional emoticons that need replacing, just add another key-value-pair, like {":D", "laughing"} to the dictionary.

As an alternative to the foreach-loop, it would also be possible (though not necessarily recommended) to use the Aggregate standard query operator:

string result = emoticons.Aggregate(":) bla :(",
                (text, emoticon) => text.Replace(emoticon.Key, emoticon.Value));


回答4:

Why regex?

 string newTweet = oldTweet
  .Replace(":)","happy")
  .Replace(":(","sad")
  .Replace(":D","even more happy");