如何在C#.net中突出显示完全匹配关键字 [英] how to highlight the exact match keyword in C#.net

查看:77
本文介绍了如何在C#.net中突出显示完全匹配关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我们正在进行文本搜索并突出显示关键字.

达到这种工作效果.


我使用了以下代码:-

Hi
we are doing text search and highlight the keyword.

up to this working good.


I used the following code:-

if (Regex.IsMatch(retext, string.Format(@"\b{0}\b";, str, RegexOptions.Singleline)) == true)

{
  Label3.Text = retext.Replace(str, string.Format("<span style="font-weight">

bold;background-color:yellow ;''> {0,1}</span>", str));
                                   

 string strdis = retext.Replace(str, string.Format("", str), RegexOptions.Singleline| 

RegexOptions.IgnoreCase);
                                    
retext = Label3.Text;
                              

}



但是问题是当iam突出显示搜索关键字时,它突出显示了精确关键字,同时突出了相关的子匹配词.


示例:-
Iam搜索关键字"net",但突出显示了单词"net","nunet"和"netherlands".

当iam用"net"搜索关键字时,必须突出显示文本中唯一的"net".
请指导我

在此先谢谢您.



But the problem is when iam highlight the search keyword,it is highlight the exact keyword also highlight the related sub matching words.


Example:-
Iam searching the keyword "net" but it highlight the words "net" and "nunet" and "netherlands".

When iam searching the keyword with "net" it has to highlight the only "net" in my text.
Please guide me

Thanks in Advance.

推荐答案

我认为\ b足以将其仅用作单词.
试试
I think \b is enough to get it as word only.
Try
retext = retext.Replace(str, String.Format("\b{0}\b",str), String.Format("<span style="font-weight:bold;background-color:yellow ;">{0,1}</span>", str))


检查此链接
它可能对您有帮助

http://stackoverflow.com/questions/3414556/regular- expression-to-match-exact-word-search-string-highlight [
Check this link
It might help you

http://stackoverflow.com/questions/3414556/regular-expression-to-match-exact-word-search-string-highlight[^]


sanjeev


为正则表达式尝试以下模式:"\< net \>".
Try this pattern for Regex: "\<net\>".


这篇关于如何在C#.net中突出显示完全匹配关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆