Replace emoticon with word in tweet using regex c#

2019-07-30 16:29发布

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.

4条回答
放我归山
2楼-- · 2019-07-30 16:55

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

str = str.Replace(":(", "text1")
         .Replace(":)", "text2")
         .Replace(":D", "text3")
查看更多
萌系小妹纸
3楼-- · 2019-07-30 16:58

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 + "]");
}
查看更多
\"骚年 ilove
4楼-- · 2019-07-30 17:09

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));
查看更多
Emotional °昔
5楼-- · 2019-07-30 17:13

Why regex?

 string newTweet = oldTweet
  .Replace(":)","happy")
  .Replace(":(","sad")
  .Replace(":D","even more happy");
查看更多
登录 后发表回答