如何用正则表达式实现如下?

tuery 2003-09-18 07:31:22
例如有一txt文件,里面的内容如下:

F:\Documents and Settings\www\桌面\复件 treednd\TreeDnD.rar
C:\windows\cvb\复件 treednd\abc.exe
F:\Documents and Settings\www\桌面\复件 treednd\sonrt.sdf

当输入abc.exe的时候,就可以把"C:\windows\cvb\复件 treednd\abc.exe"整句提取出来,请问怎样实现,先谢谢了.
...全文
50 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tuery 2003-09-18
  • 打赏
  • 举报
回复
谢谢思归大侠,我的问题解决了:)
saucer 2003-09-18
  • 打赏
  • 举报
回复
but if you insist, try

using System;
using System.IO;
using System.Text.RegularExpressions;

string sText = "abc.exe";
sText = sText.Replace(".","\\.");
StreamReader sr = new StreamReader("yourtext.txt", System.Text.Encoding.GetEncoding("GB2312"));
String sLine = sr.ReadToEnd();
sr.Close();

Regex re = new Regex(@"[^\n]*" + sText + @"\s*(?=\n|$)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
MatchCollection mc = re.Matches(sLine);
foreach (Match m in mc)
Console.WriteLine(m.Value);
saucer 2003-09-18
  • 打赏
  • 举报
回复
you don't even need Regular Expressions, just do

string sText = "abc.exe";
StreamReader sr = new StreamReader("your.txt", System.Text.Encoding.Default);
String sLine;

while ((sLine = sr.ReadLine()) != null)
{
if (sLine.EndsWith(sText))
{
Console.WriteLine(sLine);
break; // comment this line out, if there are more files for each input
}
}

sr.Close();

110,534

社区成员

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

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

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