2

我正在尝试生成一个正则表达式,它将用适当的“ <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:)"传递到目前为止提供的正则表达式,产生匹配并替换 like 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;
    }
}
4

3 回答 3

1

通过循环两次让它工作。曾经将基于符号的笑脸替换为其文本对应物。:)=[smile]例如。然后第二次仅将文本表示替换到图像中,将alt=":)"标签附加到<img>代码中。

虽然我不喜欢将整个 9 码循环两次并在输入上执行两次正则表达式,但这是一种相当简单的方法。

修改后的工作代码如下。

        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 + @""">");
        }
于 2013-09-13T03:55:07.983 回答
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;
    }
于 2014-01-17T12:14:05.003 回答
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 + @"""/»"));
}

您将不得不转义您正在制作的笑脸数组中的所有特殊字符,就像这样

":)" => "\:\)"
于 2013-09-12T05:22:40.603 回答