正则表达式获取a标签里的title

现在是直接获取整个a标签里,然后遍历输出item.groups["title"].value;
希望能修改正则表达式,直接获取title,然后输出item.value

static void Main(string[] args)
{
string resultHtml = getHtml("http://bbs.csdn.net/topics/391047173");
//获取a标签中href
//string regexHref = @"(?is)[^'""]*)[^<]*";
//获取a标签中title
string regexHrefTitleInA2 = @"(?is)<a[^>]+?title=(['""]?)(?

[^'""\s>]+)\1[^>]*>((?:(?!</?a\b).)*)</a>";
var matches = Regex.Matches(resultHtml, regexHrefTitleInA2);
foreach (Match item in matches)
{
if (item.Success)
{
Console.WriteLine(item.Groups["title"].Value);
}
Console.WriteLine();
}
Console.Read();
}
///
/// 获取指定网址内容
///
/// 网址
/// 网页内容字符串
private static string getHtml(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream stream = response.GetResponseStream();
        StreamReader sr = new StreamReader(stream,Encoding.GetEncoding(response.CharacterSet));
        string html = sr.ReadToEnd();
        sr.Close();
        return html;
    }

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐