C# 会正则表达式的帮我写两个表达式,不胜感激! 本帖最后由 zhenghaihaihai 于 2012-07-02 22:11:09 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string s = @"你的..."; Match match = Regex.Match(s, @"(?is)文件名称:.*?<td class=""row2"" width=""85%"">(?<文件名>.+?)</td>"); Console.WriteLine(match.Groups["文件名"].Value); match = Regex.Match(s, @"(?is)下载地址:.*?<a href=""(?<下载地址>.+?)"">下载</a>"); Console.WriteLine(match.Groups["下载地址"].Value);输出:非笔试类课程期末考核要求说明及评(软件工程).docfile_do.php?file_id=155909 string tempStr = File.ReadAllText(@"C:\Users\M\Desktop\Test.txt", Encoding.GetEncoding("GB2312")); string pattern = @"<td[^>]*?class=(['""])row2\1[^>]*?>([^<]*?\.[^<]*)[\s\S]*?<td[^>]*?class=(['""])row2\3[^>]*?><a[^>]*?href=(['""])([^'""]*?)\4[^>]*?>"; Match m = Regex.Match(tempStr,pattern); string fileName = m.Groups[2].Value;//非笔试类课程期末考核要求说明及评(软件工程).doc string downloadLink = m.Groups[5].Value;//file_do.php?file_id=155909 1、(?is)<tr>\s*<td\b[^>]*?><b\b[^>]*?>文件名称:</b></td>\s*<td\b[^>]*?>(?<文件名称>.*?)</td>\s*</tr>取Groups["文件名称"].Value或者Groups[1].Value2、(?is)<tr>\s*<td\b[^>]*?><b\b[^>]*?>下载地址:</b></td>\s*<td\b[^>]*?><a\s*href=(["'\s]?)([^"']*?)\1[^>]*?>.*?</a></td>\s*</tr>Groups[2].Value,注意双引号转义Regex reg=new Regex(@"(?is)<tr>\s*<td\b[^>]*?><b\b[^>]*?>下载地址:</b></td>\s*<td\b[^>]*?><a\s*href=([""'\s]?)([^""']*?)\1[^>]*?>.*?</a></td>\s*</tr>"); 1. (?is)<td.*?文件名称:.*?<td[^>]*?>(?<file>.*?)</td> 取Groups["file"].Value2.(?is)<td.*?下载地址:.*?<td[^>]*?><a[^>]*?href=(['"]?)(?<href>.*?)\1>下载</a></td>取Groups["href"].Value (?is)文件名称:.*?(?=\<td)<td[^>]+>(?<file>.*?)(?=</td>).*?下载地址:.*?<a\s+href=['"](?<url>[^'"]+)['"]Groups["file"].Value="非笔试类课程期末考核要求说明及评(软件工程).doc"Groups["url"].Value="file_do.php?file_id=155909" C#实现udp通信代码! 结束进程 comboBoxEX 控件奇怪的问题 C# 图像剪切 参数解释? 谁有用Enterprise Library 3.1写的东东,跪谢 为什么按F12转到定义时,总是显示从元数据,不能进行编辑? asp.net控件问题,急需各位帮助 同步的lock()方法怎么用?主要是LOCK(XX),这个XX是怎么定义的?? c#读取db2数据库中图片,图片显示不完整,(c#和db2数据库)。 从xml文件读取值并绑定多个控件,控件互相干扰的问题 求助 套接字通信问题!救命用 Click += new EventHandler事件的取值问题
Match match = Regex.Match(s, @"(?is)文件名称:.*?<td class=""row2"" width=""85%"">(?<文件名>.+?)</td>");
Console.WriteLine(match.Groups["文件名"].Value);
match = Regex.Match(s, @"(?is)下载地址:.*?<a href=""(?<下载地址>.+?)"">下载</a>");
Console.WriteLine(match.Groups["下载地址"].Value);
输出:
非笔试类课程期末考核要求说明及评(软件工程).doc
file_do.php?file_id=155909
string pattern = @"<td[^>]*?class=(['""])row2\1[^>]*?>([^<]*?\.[^<]*)[\s\S]*?<td[^>]*?class=(['""])row2\3[^>]*?><a[^>]*?href=(['""])([^'""]*?)\4[^>]*?>";
Match m = Regex.Match(tempStr,pattern);
string fileName = m.Groups[2].Value;//非笔试类课程期末考核要求说明及评(软件工程).doc
string downloadLink = m.Groups[5].Value;//file_do.php?file_id=155909
取Groups["文件名称"].Value或者Groups[1].Value
2、(?is)<tr>\s*<td\b[^>]*?><b\b[^>]*?>下载地址:</b></td>\s*<td\b[^>]*?><a\s*href=(["'\s]?)([^"']*?)\1[^>]*?>.*?</a></td>\s*</tr>
Groups[2].Value,注意双引号转义
Regex reg=new Regex(@"(?is)<tr>\s*<td\b[^>]*?><b\b[^>]*?>下载地址:</b></td>\s*<td\b[^>]*?><a\s*href=([""'\s]?)([^""']*?)\1[^>]*?>.*?</a></td>\s*</tr>");
取Groups["file"].Value2.(?is)<td.*?下载地址:.*?<td[^>]*?><a[^>]*?href=(['"]?)(?<href>.*?)\1>下载</a></td>
取Groups["href"].Value
Groups["url"].Value="file_do.php?file_id=155909"