如何在每行中提取含有关键词的字符串?

nanxinda88 2010-01-14 03:34:48
richTextBox1中的数据:
---------------------------------------------
……
2010-01-01 01:01:01:5158=report
2010-01-01 01:03:03:5159=result
2010-01-01 01:08:08:5160=message
2010-01-01 01:11:11:5161=report
2010-01-01 01:12:12:5162=report
2010-01-01 01:13:13:5163=message
2010-01-01 01:16:16:5164=report
2010-01-01 01:18:18:5165=result
……
---------------------------------------------

想要把含有“report”的每行都提取出来,然后保留第三个冒号之前的字符串,并在richTextBox2中列出:
---------------------------------------------
……
2010-01-01 01:01:01
2010-01-01 01:11:11
2010-01-01 01:12:12
2010-01-01 01:16:16
……
---------------------------------------------
请问应该如何编写代码?
求助高手,感激不尽!
...全文
147 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
-过客- 2010-01-14
  • 打赏
  • 举报
回复
没必要搞那么麻烦

foreach(string s in richTextBox1.Lines)
{
if (s.IndexOf("report")>0)
richTextBox2.Text += s.Remove(s.LastIndexOf(":")) + "\r\n";
}
_autotest 2010-01-14
  • 打赏
  • 举报
回复

string str = @"2010-01-01 01:01:01:5158=report
2010-01-01 01:03:03:5159=result
2010-01-01 01:08:08:5160=message
2010-01-01 01:11:11:5161=report
2010-01-01 01:12:12:5162=report
2010-01-01 01:13:13:5163=message
2010-01-01 01:16:16:5164=report
2010-01-01 01:18:18:5165=result";
StringBuilder sb = new StringBuilder();
MatchCollection mc = Regex.Matches(str, ".*");
foreach (Match m in mc)
{
string temp = m.Value;
if (temp != "")
sb.Append(temp.Substring(0, temp.LastIndexOf(":")));
}
结果:
2010-01-01 01:01:01
2010-01-01 01:11:11
2010-01-01 01:12:12
2010-01-01 01:16:16
。。。。
huming_h 2010-01-14
  • 打赏
  • 举报
回复
string[] s = richTextBox1.Text.Split('\n');
foreach (string str in s)
{
if (str.Contains("report"))
{
int index = str.LastIndexOf(':');
this.richTextBox2.AppendText(str.Substring(0,index));
}
}
_autotest 2010-01-14
  • 打赏
  • 举报
回复
matchcollection mc=regex.matches(string,".*");
foreach (match m in mc)
{
string temp=m.value;
print temp.substring(0,temp.lastindexof(":"));
}
nanxinda88 2010-01-14
  • 打赏
  • 举报
回复
都快沉了,顶出来!

111,120

社区成员

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

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

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