正则达人 帮忙写个正则 匹配网址 本帖最后由 wy811007 于 2012-06-21 15:50:55 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 StreamReader reader = new StreamReader("c:\\1.txt"); string source = reader.ReadToEnd(); Regex reg = new Regex(@"(?is)(?<num>\d+).shtml[^>]*title=""(?<title>[^>]*)"""); MatchCollection mc = reg.Matches(source); string[] ss = new string[mc.Count]; for (int i = 0; i < ss.Length; i++) { ss[i] = mc[i].Groups["num"].Value + ":" + mc[i].Groups["title"].Value; } 用Dictionary<string, string>不错 string input = @"<a href=""/xxx/76166.shtml"" target=""_blank"" title=""xx6""><a href=""/xxx/76165.shtml"" target=""_blank"" title=""xx5""><a href=""/xxx/76164.shtml"" target=""_blank"" title=""xx4""><a href=""/xxx/76163.shtml"" target=""_blank"" title=""xx3"">"; Dictionary<string, string> dic = new Dictionary<string, string>(); foreach (Match m in Regex.Matches(input, @"(?is)<a\b[^>]*?href=([""'\s]?)/xxx/(\d+)\.shtml\1[^>]*?title=([""'\s]?)([^""']*?)\3[^>]*?>")) { dic.Add(m.Groups[2].Value, m.Groups[4].Value); } foreach (var m in dic) { Console.WriteLine(m.Key + "\t" + m.Value); } string s = @"<a href=""/xxx/76166.shtml"" target=""_blank"" title=""xx6""><a href=""/xxx/76165.shtml"" target=""_blank"" title=""xx5""><a href=""/xxx/76164.shtml"" target=""_blank"" title=""xx4""><a href=""/xxx/76163.shtml"" target=""_blank"" title=""xx3"">"; MatchCollection matches = Regex.Matches(s, @"(?is)<a href=""(.*?)"".+?title=""(.*?)"">"); foreach (Match match in matches) Console.WriteLine(match.Groups[1].Value + "\t" + match.Groups[2].Value); 都是高手 谢谢了 不过这个匹配后有重复的咋弄额 我用的 bdmh 大大的 做软件测试的前辈们进来指点指点啊 请教一个统计文件夹大小问题 如何访问远程ftp目录中的一个文件啊? 如何正确注册一个C#的DLL 求一篇asp.net(c#)的论坛源码 .Net通用WebService调用方法? 求助:水晶报表分发后,出现登陆服务器对话框,但一直登陆失败。谢谢! C#动态添加combobox控件 我在DataSet里添加了一个新的表,我希望用Adapter.update可以将这个表存到数据库中,请问应该如何实现,谢谢 怎么关闭Vsiual.net的自动添加注释的功能? C# 使用JQuery zTree3.2插件报错 急 试问一下C#应用程序中如何载入第二个窗口
StreamReader reader = new StreamReader("c:\\1.txt");
string source = reader.ReadToEnd();
Regex reg = new Regex(@"(?is)(?<num>\d+).shtml[^>]*title=""(?<title>[^>]*)""");
MatchCollection mc = reg.Matches(source);
string[] ss = new string[mc.Count];
for (int i = 0; i < ss.Length; i++)
{
ss[i] = mc[i].Groups["num"].Value + ":" + mc[i].Groups["title"].Value;
}
<a href=""/xxx/76165.shtml"" target=""_blank"" title=""xx5"">
<a href=""/xxx/76164.shtml"" target=""_blank"" title=""xx4"">
<a href=""/xxx/76163.shtml"" target=""_blank"" title=""xx3"">
";
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach (Match m in Regex.Matches(input, @"(?is)<a\b[^>]*?href=([""'\s]?)/xxx/(\d+)\.shtml\1[^>]*?title=([""'\s]?)([^""']*?)\3[^>]*?>"))
{
dic.Add(m.Groups[2].Value, m.Groups[4].Value);
} foreach (var m in dic)
{
Console.WriteLine(m.Key + "\t" + m.Value);
}
<a href=""/xxx/76165.shtml"" target=""_blank"" title=""xx5"">
<a href=""/xxx/76164.shtml"" target=""_blank"" title=""xx4"">
<a href=""/xxx/76163.shtml"" target=""_blank"" title=""xx3"">";
MatchCollection matches = Regex.Matches(s, @"(?is)<a href=""(.*?)"".+?title=""(.*?)"">");
foreach (Match match in matches)
Console.WriteLine(match.Groups[1].Value + "\t" + match.Groups[2].Value);