c#正则表达式.替换为现有单词
本文关键字:单词 正则表达式 替换 | 更新日期: 2023-09-27 17:53:09
我试图使用Regex在文本字符串中突出显示文本。替换它,但是当我搜索"问题"这个词时,我希望"问题"也突出显示,而不是"s"。它突出了"现在",但用"问题"代替了"问题"。我如何知道当前匹配是否以"s"结尾?这就是我要用的
e.Row.Cells[6].Text = Regex.Replace(
e.Row.Cells[6].Text,
"''b" + Session["filterWord"].ToString() + "[s]{0,1}''b",
"<b><font color='"red'">" + Session["filterWord"].ToString() + "</font></b>",
RegexOptions.IgnoreCase);
使用以下(捕获组):
e.Row.Cells[6].Text = Regex.Replace(
e.Row.Cells[6].Text,
"''b" + Session["filterWord"].ToString() + "([s]?)''b",
"<b><font color='"red'">" + Session["filterWord"].ToString() + "$1</font></b>",
RegexOptions.IgnoreCase);