C # Regex.Replace with existing word

I'm trying to highlight text in a line of text using Regex.Replace, which works, but when I search for the word "problem" I want the "problems" to also highlight just not the "s". It stands out right now, but replaces "problems" with "problem". How do I know if the current match is "s" at the end? This is what I am 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);

      

+3


source to share


1 answer


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

      

+4


source







All Articles