用<img/>标记替换笑脸文本

时间:2013-09-12 03:10:13

标签: c# regex linq

我正在尝试生成一个正则表达式,它将用适当的“<img src="blah" alt="blah">”代码替换OrderedDictionary的值。

因为它还输入了表情符号的alt=""文本版本,所以它匹配两次,将所有内容都搞砸了。

我目前的尝试如下。

foreach (string smiley in Smilies.Keys)
{
    Regex re = new Regex("(?<!(=\"))" + Smilies[smiley] + "(?<!(\"))");
    Trace.WriteLine(re.Replace(msg, @"«img src=""" + path + @"\images\" + Smilies[smiley] + @".gif"" alt=""" + smiley + @"""/»"));
}

表情符号在这里:

        Smilies = new OrderedDictionary();
        Smilies.Add(@"O:)", "angel");
        Smilies.Add(@":-x", "cheekkiss");
        Smilies.Add(@":\", "chin");
        Smilies.Add(@"B)", "cool");
        Smilies.Add(@":~", "crutches");
        Smilies.Add(@"«3", "love");
        Smilies.Add(@"»:(", "mad");
        Smilies.Add(@"):«", "mad");
        Smilies.Add(@":o", "ohmy");
        Smilies.Add(@"@(", "ouch");
        Smilies.Add(@":)", "smile");
        Smilies.Add(@"^_^", "smug");
        Smilies.Add(@"^.^", "smug");
        Smilies.Add(@":+", "therethere");
        Smilies.Add(@":P", "tongue_out");

编辑:

这在大多数情况下目前正在发挥作用。虽然我认为我将表情符号转换为图像的方法存在缺陷。

O:) => alt="O:)"传递正则表达式提供的sofar,产生匹配并替换为 alt="O:)" => alt="Oalt=":)""

也许我是以错误的方式来到这里的?

foreach (string smiley in Smilies.Keys)
{
    string s = Smilies[smiley].ToString();
    string pattern = @"(?<!(=""))" + Regex.Escape(smiley) + @"(?<!(""))";
    string rep = @"«img src=""" + path + @"\images\" + s + @".gif"" alt=""" + smiley + @"""/»";
    Regex re = new Regex(pattern);
    if (re.IsMatch(msg))
    {
        Trace.WriteLine("Got a match!");
        string m2 = re.Replace(msg, rep);
        msg = m2;
    }
}

3 个答案:

答案 0 :(得分:1)

通过循环两次来完成它。一旦将符号为基础的笑脸替换为他们的文本对应物。例如:) = [smile]。然后第二次将文本表示替换为图像,将alt=":)"标记附加到<img>代码。

虽然我不喜欢将整个9码环绕两次并且在输入上做两次RegEx,但这是一种相当简单的方法。

修改后的工作代码如下。

        foreach (string smiley in Smilies.Keys)
        {
            Regex re = new Regex(Regex.Escape(smiley));
            msg = re.Replace(msg, "[" + Smilies[smiley] + "]");
        }

        foreach (string smiley in Smilies.Keys)
        {
            Regex re = new Regex(@"\[" + Regex.Escape(Smilies[smiley].ToString()) + @"\]");
            msg = re.Replace(msg, @"<img src=""" + Application.StartupPath + @"\images\" + Smilies[smiley] + @".gif"" alt=""" + smiley + @""">");
        }

答案 1 :(得分:0)

这应该匹配并正常工作。

foreach (string smiley in Smilies.Keys)
{
  Regex re = new Regex("(?<=\")" + Smilies[smiley] + "(?=\")");
  Trace.WriteLine(re.Replace(msg, @"«img src=""" + path + @"\images\" + Smilies[smiley] + @".gif"" alt=""" + smiley + @"""/»"));
}

你将不得不逃脱你正在制作它的笑脸阵列中的所有特殊字符,比如

":)" => "\:\)"

答案 2 :(得分:0)

    public static string MakeSomeSmilies(string incomingtext)
    {
        var smilies = new OrderedDictionary
        {
            {@"O:)", "angel"},
            {@":-x", "cheekkiss"},
            {@":\", "chin"},
            {@"B)", "cool"},
            {@":~", "crutches"},
            {@"«3", "love"},
            {@"»:(", "mad"},
            {@"):«", "mad"},
            {@":o", "ohmy"},
            {@"@(", "ouch"},
            {@":)", "smile"},
            {@"^_^", "smug"},
            {@"^.^", "smug"},
            {@":+", "therethere"},
            {@":P", "tongue_out"}
        };
        foreach (string m2 in from string smiley in smilies.Keys let re = new Regex(@"(?<!(=""))" + Regex.Escape(smiley) + @"(?<!(""))") where re.IsMatch(incomingtext) select re.Replace(incomingtext, @"«img src=""" + "sd" + @"\images\" + smilies[smiley] + @".gif"" alt=""" + smiley + @"""/»"))
        {
            incomingtext = m2;
        }
        return incomingtext;
    }