生成在另一个字符串中找到的突出显示的字符串的最佳方法是什么?如何突出显示忽略空格和非字母数字字符的字符串内的字符串?
我想忽略所有不是字母数字的字符,但将它们保留在最终输出中。
因此,例如在以下3个字符串“PC3000”的搜索将给出以下结果:
ZxPc 3000L = Zx<font color='red'>Pc 3000</font>L
ZXP-C300-0Y = ZX<font color='red'>P-C300-0</font>Y
Pc3 000 = <font color='red'>Pc3 000</font>
我有以下的代码,但我可以突出显示搜索中的结果是唯一的出路删除所有空白字符和非字母数字字符,然后将这两个字符串设置为小写字母。我卡住了!
public string Highlight(string Search_Str, string InputTxt)
{
// Setup the regular expression and add the Or operator.
Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
// Highlight keywords by calling the delegate each time a keyword is found.
string Lightup = RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));
if (Lightup == InputTxt)
{
Regex RegExp2 = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
RegExp2.Replace(" ", "");
Lightup = RegExp2.Replace(InputTxt.Replace(" ", ""), new MatchEvaluator(ReplaceKeyWords));
int Found = Lightup.IndexOf("<font color='red'>");
if (Found == -1)
{
Lightup = InputTxt;
}
}
RegExp = null;
return Lightup;
}
public string ReplaceKeyWords(Match m)
{
return "<font color='red'>" + m.Value + "</font>";
}
谢谢你们!
您能否提供一个输入和输出的例子?我假设基地是“剥离html标签,然后找到字符串,即使它有空格”,我说得对吗?另外,'突出显示'你的意思是红色? – Gabber