字符串是:……
<div class=brs_col>
<p><a href="aaa.htm" ><b>microsoft</b> access</a></p>
<p><a href="bbb.htm" >access <b>hollywood</b></a></p>
</div>
<div class=brs_col>
<p><a href="ccc.htm" >access <b>2003</b></a></p>
<p><a href="ddd.htm" ><b>learn</b> access</a></p>
</div>
……我想取出来所有连接的地址,也就是x.htm和连接的纯文字,不要<b>标签。然后放到一个数组中。类似于这样,网上找了个代码,但是出错,实在不会玩正则。
Regex reg = new Regex(@"<a[^>]*?href="(?<url>[^"]*)"[^>]*>(?<title>.*?)</a>");
Match m = reg.Match(目前字符串);
if(m.Success)
{
m.Group["url"].Value //就是 获取的uRL
m.Group["title"].Value //就是 获取的标题
}
<div class=brs_col>
<p><a href="aaa.htm" ><b>microsoft</b> access</a></p>
<p><a href="bbb.htm" >access <b>hollywood</b></a></p>
</div>
<div class=brs_col>
<p><a href="ccc.htm" >access <b>2003</b></a></p>
<p><a href="ddd.htm" ><b>learn</b> access</a></p>
</div>
……我想取出来所有连接的地址,也就是x.htm和连接的纯文字,不要<b>标签。然后放到一个数组中。类似于这样,网上找了个代码,但是出错,实在不会玩正则。
Regex reg = new Regex(@"<a[^>]*?href="(?<url>[^"]*)"[^>]*>(?<title>.*?)</a>");
Match m = reg.Match(目前字符串);
if(m.Success)
{
m.Group["url"].Value //就是 获取的uRL
m.Group["title"].Value //就是 获取的标题
}
那去掉<b>标签的操作没有啊
MatchCollection mc = Regex.Matches(yourHtml,@"(?is)<a.+?href=(\x22?)(?<url>.+?)\1[^>]*>(?<title>.+?)</a>");
foreach(Match m in mc)
{
string url = m.Groups["url"].Value;
string title = Regex.Replace(m.Groups["title"].Value,"<[^>]*>","");
}
string text = @"<p><a href=""aaa.htm"" ><b>microsoft</b> access</a></p>";
string pattern = @"(?is)<a\s+href=""(?<url>[^""]*?)""\s*?>(?<title>.*?)</a>";
MatchCollection mc = Regex.Matches(text, pattern);
string url;
string title;
foreach (Match m in mc)
{
url = m.Groups["url"].Value;
title = m.Groups["title"].Value.Replace("<b>", "");
title = title.Replace("</b>", "");
Console.WriteLine(url + ":" + title);
}