2

我有一个问题orderedDictionaryRegex。如何通过文本定义问题,但我会尝试解释......

这是我的代码:

var smilies = new OrderedDictionary
{
    {@":(", "smile_sadx24"},
    {@">:(", "smile_angryx24"},
};

foreach (string smiley in smilies.Keys)
{
    var re = new Regex(@"(?<!(=""))" + Regex.Escape(smiley) + @"(?<!(""))");
    if (re.IsMatch(incomingtext))
    {
        string m2 = re.Replace(incomingtext, "<img src=\"/Content/themes/mavi/img/custom/smiles/" + smilies[smiley] + ".png\"  alt=\"" + smiley + "\"/>");
        incomingtext = m2;
    }
}

当我试图处理>:(然后它返回smile_sadx24smile_angryx24...

因为它:(先捕获...

我怎么解决这个问题?

4

1 回答 1

1

两件事情:

var smilies = new OrderedDictionary
{
    {@">:(", "smile_angryx24"},  // Put this one first
    {@":(", "smile_sadx24"},
};

foreach (string smiley in smilies.Keys)
{
    var re = new Regex(@"(?<!(=""))" + Regex.Escape(smiley) + @"(?!(""))"); 
                       // Second should be a negative lookahead  ^^
    if (re.IsMatch(incomingtext))
    {
        string m2 = re.Replace(incomingtext, "<img src=\"/Content/themes/mavi/img/custom/smiles/" + smilies[smiley] + ".png\"  alt=\"" + smiley + "\"/>");
        incomingtext = m2;
    }
}

ideone演示

当你交换愤怒和悲伤的笑脸时,在应该向前看的地方使用消极的眼神是导致正则表达式失败的原因,因为笑脸字符根本没有使用"

于 2014-01-18T17:25:24.267 回答