62,025
社区成员
发帖
与我相关
我的任务
分享
void Main()
{
string html = @"<a href=""http://www.baidu.com"" title=""百度一下"">百度</a>""";
foreach(Match m in Regex.Matches(html,@"(?i)<a[^>]*href=(['""]?)(?<href>[^'""]+)\1[^>]*title=\1(?<title>[^'""]+)\1[^>]*>(?<value>[^<]+)</a>"))
{
Console.WriteLine("href: {0} title: {1} value: {2}",m.Groups["href"].Value,m.Groups["title"].Value,m.Groups["value"].Value);
}
//href: http://www.baidu.com title: 百度一下 value: 百度
}
string str = "<a href=\"http://www.baidu.com\" title=\"百度一下\">百度</a>";
Regex re = new Regex(@"<a\s*href=\""(?<href>.*?)\""\s*title=\""(?<title>.*?)\"">(?<content>.*?)</a>", RegexOptions.None);
MatchCollection mc = re.Matches(str);
foreach (Match ma in mc)
{
//ma.Groups["href"].Value 这是href,结果:http://www.baidu.com
//ma.Groups["title"].Value 这是title,结果:百度一下
//ma.Groups["content"].Value) 这是A标签里的值,结果:百度
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string html = @":<a href=""http://www.baidu.com"" title=""百度一下"">百度</a>""";
foreach (Match m in Regex.Matches(html, @"(?i)<(a)\s*[^=]+?=""(?<href>[^""]+)""\s*[^=]+?=""(?<title>[^""]+)"">(?<a>[^>]*?)</\1>"))
{
foreach (Capture c in m.Groups["href"].Captures)
{
Console.WriteLine(c.Value);
}
Console.WriteLine("*************");
foreach (Capture c in m.Groups["title"].Captures)
{
Console.WriteLine(c.Value);
}
Console.WriteLine("*************");
foreach (Capture c in m.Groups["a"].Captures)
{
Console.WriteLine(c.Value);
}
Console.WriteLine("*************");
}
}
}
}