帮忙看下这个正则表达式怎么写 <a href='地址' class='BookName' title='可变'>名称</a>请问下,我想获取地址 和名称。在asp.net中应该如何写我试了写了下在测试的工具中可以,到asp.net中就是不行 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.Text;using System.Text.RegularExpressions;class Test{ static void Main() { string strHyerLink = "<a href='地址' class='BookName' title='可变'>名称 </a> "; Match m = Regex.Match(strHyerLink, "href=['\"]?(.*?)['\" ].*?>(.*?)</"); bool bl = m.Success; if (bl) { Console.WriteLine("OK"); Console.WriteLine(m.Groups[1].Value); Console.WriteLine(m.Groups[2].Value); } Console.ReadKey(); }} 再问下,如果我想取出 地址 和名称 应该如何写?下面的是我写的,不知道为什么在工具里能匹配到,在asp.net里就匹配不到<a\s?href='(.*)'\s?class='BookName'\s?title='(.*)'>(.*)</a> string strHyerLink = "<a href='地址' class='BookName' title='可变'>名称 </a> "; Match m = Regex.Match(strHyerLink, @"<a\s?href='(.*)'\s?class='BookName'\s?title='(.*)'>(.*) </a>"); //你这表达式虽然可捕获结构但是扩展性太差源代码稍稍改动你就得重写 bool bl = m.Success; if (bl) { Console.WriteLine("OK"); Console.WriteLine(m.Groups[1].Value); Console.WriteLine(m.Groups[2].Value); } Console.ReadKey(); string strHtml = "<a href='1038075,21072281.aspx' class='BookName' title='字数:5743 更新时间:2008-8-21 18:13:42'> 第三十三章节火神殿</a></td></tr><tr><td style='width:33%' class='chaptertd'><a href='1038075,21081291.aspx' class='BookName' title='字数:5665 更新时间:2008-8-22 19:58:34'> 第三十四章节神冢</a></td><td style='width:33%' class='chaptertd'><a href='1038075,21088439.aspx' class='BookName' title='字数:6238 更新时间:2008-8-23 19:39:46'> 第三十五章节青莲妙风</a></td><td style='width:33%' class='chaptertd'><a href='1038075,21096401.aspx' class='BookName' title='字数:4407 更新时间:2008-8-24 20:10:02'> 第三十六章节背叛</a>"; MatchCollection mc = Regex.Matches(strHtml, @"<a.*?href=['""]?(.*?)['"" ]+.*?title=['""]?(.*?)['""]+.*?>(.*?)</"); foreach (Match m in mc) { Response.Write(m.Groups[1].Value); Response.Write(m.Groups[2].Value); Response.Write(m.Groups[3].Value); } 【异想天开】 asp.net 网站可以实现“热插拔”功能吗 .Net问题。。。通过一个HyperLinkField连接到显示教材的详细信息页面时,怎样把教材的图片也显示出来??? 求mvc3.0网站 Asp.Net 上传文件太大无响应 网页的图片为什么有时候显示不出来? 如何实现跳转功能??? IsPostBack的问题 如何学好.net? 怎样在C#中获得object对象的实际表示对象? 这个xml为什么错??? 如何在ASP.NET的页面中获取到传过来的值? 文件上传问题 属于power users
using System.Text;
using System.Text.RegularExpressions;class Test
{
static void Main()
{
string strHyerLink = "<a href='地址' class='BookName' title='可变'>名称 </a> ";
Match m = Regex.Match(strHyerLink, "href=['\"]?(.*?)['\" ].*?>(.*?)</");
bool bl = m.Success;
if (bl)
{
Console.WriteLine("OK");
Console.WriteLine(m.Groups[1].Value);
Console.WriteLine(m.Groups[2].Value);
}
Console.ReadKey();
}
}
<a\s?href='(.*)'\s?class='BookName'\s?title='(.*)'>(.*)</a>
Match m = Regex.Match(strHyerLink, @"<a\s?href='(.*)'\s?class='BookName'\s?title='(.*)'>(.*) </a>");
//你这表达式虽然可捕获结构但是扩展性太差源代码稍稍改动你就得重写
bool bl = m.Success;
if (bl)
{
Console.WriteLine("OK");
Console.WriteLine(m.Groups[1].Value);
Console.WriteLine(m.Groups[2].Value);
}
Console.ReadKey();
MatchCollection mc = Regex.Matches(strHtml, @"<a.*?href=['""]?(.*?)['"" ]+.*?title=['""]?(.*?)['""]+.*?>(.*?)</");
foreach (Match m in mc)
{
Response.Write(m.Groups[1].Value); Response.Write(m.Groups[2].Value);
Response.Write(m.Groups[3].Value);
}