Link to home
Start Free TrialLog in
Avatar of Tourist_Search
Tourist_Search

asked on

Word boundarys and highlight text using regular expressions

Hi
Can anyone help with this small problem.

I can get the text to be highlighted with a match but I'm trying to use word boundaries so that only whole words are highlighted.

My code is:

highlightphrase = Regex.Replace(highlightphrase, "day", "<strong>day</strong>", RegexOptions.IgnoreCase|RegexOptions.Singleline);

Problem is the above is highlighting day in holiday which is incorrect.

I've tried using \< but receive an error stating non reconized escape character also tried using \b but still does not work. I'm using C# and running asp.net.

Any help would be appreciated.
George
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Tourist_Search,
Use this :
highlightphrase=Regex.Replace(highlightphrase,"(\\bday\\b)","<strong>$1</strong>", RegexOptions.IgnoreCase|RegexOptions.Singleline);

Regards
x_com
            
Avatar of Tourist_Search
Tourist_Search

ASKER

Thanks x_com

That worked OK, should have asked how to acheive results when value is a variable.

FirstWord is variable

highlightphrase=Regex.Replace(highlightphrase,"(\\bFirstWord\\b)","<strong>$1</strong>", RegexOptions.IgnoreCase|RegexOptions.Singleline);

George
ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks x_com

That works OK, having problems trying to get results to display in item template, but results do display outsite of template.

George
Glad to help.
>>..having problems trying to get results to display in item template, but results do display outsite of template.
-What control you use to display the item?
Hi x_com

When I could not get the code to work, I removed it from page.
I tried putting the code inside void PreRender(object sender, EventArgs e), void Page_Init(object sender, System.EventArgs e) and void Page_Load(object sender, System.EventArgs e) but neither work.

George

Tourist_Search,
You can use OnItemBound event to solve the problem. Have you try it? Sorry for my delay because busy around this few day..
If you got any problem, you can state it clearly and i try to solve it as soon as i can.
Hi x_com

Sorry for delay, thanks for your help

George