简体   繁体   English

C#Regex.Replace with existing word

[英]C# Regex.Replace with existing word

I am trying to highlight text in a string of text using Regex.Replace which is working, but when I search for the word "problem" I want "problems" to also highlight just not the "s". 我正在尝试使用正在工作的Regex.Replace来突出显示文本字符串中的文本,但是当我搜索“问题”这个词时,我希望“问题”也突出显示不是“s”。 It highlights right now but replaces "problems" with "problem". 它现在突出显示,但用“问题”取代“问题”。 How can I know if the current match has an "s" at the end? 我怎么知道当前的比赛最后是否有“s”? This is what I'm using 这就是我正在使用的

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);

Use the following (capture groups): 使用以下(捕获组):

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM