关键字高亮

fengxiazhen8636 2017-07-02 04:33:27
目前在做关键字检索时,要能完成对逗号隔开的多个关键字的检索,但是在richtextbox2输出结果中的高亮显示时会同时高亮之后要检索的词,希望最后实现每搜索一个词都只在句子中对这个词描红


代码如下:
private void find_string()//关键字
{

string keyword = comboBox1.Text;
string[] strArr = keyword.Split(',');

foreach (string s in strArr)
{

string str5 = richTextBox1.Text;
string str2 = ":";

int len = 20;

Regex reg = new Regex(s); //定义正则表达式 匹配

string result1 = reg.Match(str5).Value;
Regex reg0 = new Regex(string.Format(@"(?<prefix>[\s\S]{1})(?<key>{0})(?<suffix>[\s\S]{2})", s, "{0," + len + "}", "{0," + len + "}"));
MatchCollection mc0 = reg0.Matches(richTextBox1.Text);
//设置序号
int index = 1;
foreach (Match m0 in mc0) //循环匹配到的字符
{

this.richTextBox2.AppendText(index.ToString() + str2.ToString() + m0.Groups["prefix"] + m0.Groups["key"] + m0.Groups["suffix"] + "..." + Environment.NewLine);
index++;
}

//关键字高亮
MatchCollection mc = reg.Matches(richTextBox1.Text);
foreach (Match m in mc)
{
richTextBox1.SelectionStart = m.Index;
richTextBox1.SelectionLength = m.Length;
richTextBox1.SelectionColor = Color.Red;
}
//关键字高亮检索栏
MatchCollection mc1 = reg.Matches(richTextBox2.Text);
foreach (Match m1 in mc1)
{
richTextBox2.SelectionStart = m1.Index;
richTextBox2.SelectionLength = m1.Length;
richTextBox2.SelectionColor = Color.Blue;
}
}
}
...全文
205 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengxiazhen8636 2017-07-03
  • 打赏
  • 举报
回复
引用 5 楼 From_TaiWan 的回复:
string keyword = comboBox1.Text; string[] strArr = keyword.Split(','); string s = strArr.Take(1).ToString(); //foreach (string s in strArr) //注解掉foreach { string str5 = richTextBox1.Text; string str2 = ":"; int len = 20; Regex reg = new Regex(s); //定义正则表达式 匹配 string result1 = reg.Match(str5).Value; Regex reg0 = new Regex(string.Format(@"(?<prefix>[\s\S]{1})(?<key>{0})(?<suffix>[\s\S]{2})", s, "{0," + len + "}", "{0," + len + "}")); MatchCollection mc0 = reg0.Matches(richTextBox1.Text); //设置序号 int index = 1; foreach (Match m0 in mc0) //循环匹配到的字符 { this.richTextBox2.AppendText(index.ToString() + str2.ToString() + m0.Groups["prefix"] + m0.Groups["key"] + m0.Groups["suffix"] + "..." + Environment.NewLine); index++; } //关键字高亮 MatchCollection mc = reg.Matches(richTextBox1.Text); foreach (Match m in mc) { richTextBox1.SelectionStart = m.Index; richTextBox1.SelectionLength = m.Length; richTextBox1.SelectionColor = Color.Red; } //关键字高亮检索栏 MatchCollection mc1 = reg.Matches(richTextBox2.Text); foreach (Match m1 in mc1) { richTextBox2.SelectionStart = m1.Index; richTextBox2.SelectionLength = m1.Length; richTextBox2.SelectionColor = Color.Blue; } }
不能用啊。连检索结果都不出了
秋的红果实 2017-07-03
  • 打赏
  • 举报
回复
你不是说只要一个么
fengxiazhen8636 2017-07-03
  • 打赏
  • 举报
回复
引用 8 楼 From_TaiWan 的回复:
[quote=引用 6 楼 fengxiazhen8636 的回复:] [quote=引用 5 楼 From_TaiWan 的回复:] string keyword = comboBox1.Text; string[] strArr = keyword.Split(','); string s = strArr.Take(1).ToString(); //foreach (string s in strArr) //注解掉foreach { string str5 = richTextBox1.Text; string str2 = ":"; int len = 20; Regex reg = new Regex(s); //定义正则表达式 匹配 string result1 = reg.Match(str5).Value; Regex reg0 = new Regex(string.Format(@"(?<prefix>[\s\S]{1})(?<key>{0})(?<suffix>[\s\S]{2})", s, "{0," + len + "}", "{0," + len + "}")); MatchCollection mc0 = reg0.Matches(richTextBox1.Text); //设置序号 int index = 1; foreach (Match m0 in mc0) //循环匹配到的字符 { this.richTextBox2.AppendText(index.ToString() + str2.ToString() + m0.Groups["prefix"] + m0.Groups["key"] + m0.Groups["suffix"] + "..." + Environment.NewLine); index++; } //关键字高亮 MatchCollection mc = reg.Matches(richTextBox1.Text); foreach (Match m in mc) { richTextBox1.SelectionStart = m.Index; richTextBox1.SelectionLength = m.Length; richTextBox1.SelectionColor = Color.Red; } //关键字高亮检索栏 MatchCollection mc1 = reg.Matches(richTextBox2.Text); foreach (Match m1 in mc1) { richTextBox2.SelectionStart = m1.Index; richTextBox2.SelectionLength = m1.Length; richTextBox2.SelectionColor = Color.Blue; } }
不能用啊。连检索结果都不出了[/quote] 奥,这样改下,应该没问题了 string keyword = comboBox1.Text; string[] strArr = keyword.Split(','); string s = strArr.First(); //foreach (string s in strArr) //注解掉foreach { string str5 = richTextBox1.Text; string str2 = ":"; int len = 20;[/quote] 这个只能检索出一个词,如果想搜第二个词就不行了
秋的红果实 2017-07-03
  • 打赏
  • 举报
回复
引用 6 楼 fengxiazhen8636 的回复:
[quote=引用 5 楼 From_TaiWan 的回复:] string keyword = comboBox1.Text; string[] strArr = keyword.Split(','); string s = strArr.Take(1).ToString(); //foreach (string s in strArr) //注解掉foreach { string str5 = richTextBox1.Text; string str2 = ":"; int len = 20; Regex reg = new Regex(s); //定义正则表达式 匹配 string result1 = reg.Match(str5).Value; Regex reg0 = new Regex(string.Format(@"(?<prefix>[\s\S]{1})(?<key>{0})(?<suffix>[\s\S]{2})", s, "{0," + len + "}", "{0," + len + "}")); MatchCollection mc0 = reg0.Matches(richTextBox1.Text); //设置序号 int index = 1; foreach (Match m0 in mc0) //循环匹配到的字符 { this.richTextBox2.AppendText(index.ToString() + str2.ToString() + m0.Groups["prefix"] + m0.Groups["key"] + m0.Groups["suffix"] + "..." + Environment.NewLine); index++; } //关键字高亮 MatchCollection mc = reg.Matches(richTextBox1.Text); foreach (Match m in mc) { richTextBox1.SelectionStart = m.Index; richTextBox1.SelectionLength = m.Length; richTextBox1.SelectionColor = Color.Red; } //关键字高亮检索栏 MatchCollection mc1 = reg.Matches(richTextBox2.Text); foreach (Match m1 in mc1) { richTextBox2.SelectionStart = m1.Index; richTextBox2.SelectionLength = m1.Length; richTextBox2.SelectionColor = Color.Blue; } }
不能用啊。连检索结果都不出了[/quote] 奥,这样改下,应该没问题了 string keyword = comboBox1.Text; string[] strArr = keyword.Split(','); string s = strArr.First(); //foreach (string s in strArr) //注解掉foreach { string str5 = richTextBox1.Text; string str2 = ":"; int len = 20;
threenewbee 2017-07-03
  • 打赏
  • 举报
回复
http://blog.csdn.net/foreverling/article/details/38371867
秋的红果实 2017-07-02
  • 打赏
  • 举报
回复
string keyword = comboBox1.Text; string[] strArr = keyword.Split(','); string s = strArr.Take(1).ToString(); //foreach (string s in strArr) //注解掉foreach { string str5 = richTextBox1.Text; string str2 = ":"; int len = 20; Regex reg = new Regex(s); //定义正则表达式 匹配 string result1 = reg.Match(str5).Value; Regex reg0 = new Regex(string.Format(@"(?<prefix>[\s\S]{1})(?<key>{0})(?<suffix>[\s\S]{2})", s, "{0," + len + "}", "{0," + len + "}")); MatchCollection mc0 = reg0.Matches(richTextBox1.Text); //设置序号 int index = 1; foreach (Match m0 in mc0) //循环匹配到的字符 { this.richTextBox2.AppendText(index.ToString() + str2.ToString() + m0.Groups["prefix"] + m0.Groups["key"] + m0.Groups["suffix"] + "..." + Environment.NewLine); index++; } //关键字高亮 MatchCollection mc = reg.Matches(richTextBox1.Text); foreach (Match m in mc) { richTextBox1.SelectionStart = m.Index; richTextBox1.SelectionLength = m.Length; richTextBox1.SelectionColor = Color.Red; } //关键字高亮检索栏 MatchCollection mc1 = reg.Matches(richTextBox2.Text); foreach (Match m1 in mc1) { richTextBox2.SelectionStart = m1.Index; richTextBox2.SelectionLength = m1.Length; richTextBox2.SelectionColor = Color.Blue; } }
fengxiazhen8636 2017-07-02
  • 打赏
  • 举报
回复
引用 3 楼 From_TaiWan 的回复:
在combobox中加入一项“图像”,选“图像”,执行查询,就是只有图像高亮了
可是我们需要实现的就是同时搜多个词
秋的红果实 2017-07-02
  • 打赏
  • 举报
回复
在combobox中加入一项“图像”,选“图像”,执行查询,就是只有图像高亮了
fengxiazhen8636 2017-07-02
  • 打赏
  • 举报
回复
引用 1 楼 From_TaiWan 的回复:
没理解你的意思?
就是在搜图像这个词时只描红这一个词,不描红智能
秋的红果实 2017-07-02
  • 打赏
  • 举报
回复
没理解你的意思?

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧