C# 会正则表达式的帮我写两个表达式,不胜感激!

zhenghaihaihai 2012-07-02 10:04:41
<tr>

<td width="737" align="center" height="100%"><table class="tablebg" width="98%" align="center" cellspacing="1" cellpadding="3">

<tr>

<td class="row1" width="15%"><b class="gensmall">文件名称:</b></td>

<td class="row2" width="85%">非笔试类课程期末考核要求说明及评(软件工程).doc</td>

</tr>

<tr>

<td class="row1"><b class="gensmall">所属教师:</b></td>

<td class="row2">xx</td>

</tr>

<tr>

<td class="row1"><b class="gensmall">文件大小:</b></td>

<td class="row2">33 KB</td>

</tr>

<tr>

<td class="row1"><b class="gensmall">添加时间:</b></td>

<td class="row2">2012-05-24 10:42</td>

</tr>

<tr>

<td class="row1"><b class="gensmall">下载地址:</b></td>

<td class="row2"><a href="file_do.php?file_id=155909">下载</a></td>

</tr>

<tr>

<td class="row1" valign="top"><b class="gensmall">文件简介:</b></td>

<td class="row2"></td>

</tr>

</table>

</td>

</tr>
帮我写两个表达式,第一个:提取(文件名称:)下的文件名(如:上面的非笔试类课程期末考核要求说明及评(软件工程).doc这个文件名)。第二个:提取下载后面的那个超链接(如:<a href="file_do.php?file_id=155909">下载</a>提取出file_do.php?file_id=155909)
...全文
110 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
newxdlysk 2012-07-02
  • 打赏
  • 举报
回复
(?is)文件名称:.*?(?=\<td)<td[^>]+>(?<file>.*?)(?=</td>).*?下载地址:.*?<a\s+href=['"](?<url>[^'"]+)['"]

Groups["file"].Value="非笔试类课程期末考核要求说明及评(软件工程).doc"
Groups["url"].Value="file_do.php?file_id=155909"
q107770540 2012-07-02
  • 打赏
  • 举报
回复
1. (?is)<td.*?文件名称:.*?<td[^>]*?>(?<file>.*?)</td>
取Groups["file"].Value

2.(?is)<td.*?下载地址:.*?<td[^>]*?><a[^>]*?href=(['"]?)(?<href>.*?)\1>下载</a></td>
取Groups["href"].Value
EnForGrass 2012-07-02
  • 打赏
  • 举报
回复
1、(?is)<tr>\s*<td\b[^>]*?><b\b[^>]*?>文件名称:</b></td>\s*<td\b[^>]*?>(?<文件名称>.*?)</td>\s*</tr>
取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>");
  • 打赏
  • 举报
回复
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
dalmeeme 2012-07-02
  • 打赏
  • 举报
回复
		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);

输出:
非笔试类课程期末考核要求说明及评(软件工程).doc
file_do.php?file_id=155909

110,500

社区成员

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

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

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