110,533
社区成员
发帖
与我相关
我的任务
分享
string inputs = "<div class=\"box_01\"> <a href=\"http://tech.sina.com.cn/digi/dc/2011-05-18/09425539462.shtml\" target=\"_blank\"><img src=\"http://i1.sinaimg.cn/IT/U5311P2T1D5539462F2755DT20110518095231.jpg\" width=\"135\" height=\"85\" alt=\"徕卡昂贵镜头遭遇切片\" /></a><h3><a href=\"http://tech.sina.com.cn/digi/dc/2011-05-18/09425539462.shtml\" target=\"_blank\">徕卡昂贵镜头遭遇切片</a></h3><p>最近,国外某大学的学生为了自己的毕业设计,...</p> </div>";
string patterns = @"(?is)((href|src)=(['""])*([^\s]+?)\3)|(<p>(.*?)</p>)|(alt=(['""])*([^\s]+?)\8)";
MatchCollection matches = Regex.Matches(inputs, patterns);
foreach (Match match in matches)
{
if (!string.IsNullOrEmpty(match.Groups[2].Value))
{
Console.WriteLine("type:\t{0}", match.Groups[2].Value);
Console.WriteLine("href|src:\t{0}", match.Groups[4].Value);
}
else if (!string.IsNullOrEmpty(match.Groups[5].Value))
{
Console.WriteLine("type:\tp");
Console.WriteLine("Content:\t{0}", match.Groups[6].Value);
}
else if (!string.IsNullOrEmpty(match.Groups[7].Value))
{
Console.WriteLine("type:\talt");
Console.WriteLine("alt:\t{0}", match.Groups[9].Value);
}
Console.WriteLine();
}
/*
type: href
href|src: http://tech.sina.com.cn/digi/dc/2011-05-18/09425539462.shtml
type: src
href|src: http://i1.sinaimg.cn/IT/U5311P2T1D5539462F2755DT20110518095231.jpg
type: alt
alt: 徕卡昂贵镜头遭遇切片
type: href
href|src: http://tech.sina.com.cn/digi/dc/2011-05-18/09425539462.shtml
type: p
Content: 最近,国外某大学的学生为了自己的毕业设计,...
*/
string inputs = "<div class=\"box_01\"> <a href=\"http://tech.sina.com.cn/digi/dc/2011-05-18/09425539462.shtml\" target=\"_blank\"><img src=\"http://i1.sinaimg.cn/IT/U5311P2T1D5539462F2755DT20110518095231.jpg\" width=\"135\" height=\"85\" alt=\"徕卡昂贵镜头遭遇切片\" /></a><h3><a href=\"http://tech.sina.com.cn/digi/dc/2011-05-18/09425539462.shtml\" target=\"_blank\">徕卡昂贵镜头遭遇切片</a></h3><p>最近,国外某大学的学生为了自己的毕业设计,...</p> </div>";
string patterns = @"(?is)(href|src|alt)=+([""'^#][\w\S]*[""'>])";
MatchCollection matches = Regex.Matches(inputs, patterns);
foreach (Match match in matches)
{
Console.WriteLine("type: {0}", match.Groups[1].Value);
Console.WriteLine("href: {0}", match.Groups[2].Value);
Console.WriteLine();
}
string inputs = "<div class=\"box_01\"> <a href=\"http://tech.sina.com.cn/digi/dc/2011-05-18/09425539462.shtml\" target=\"_blank\"><img src=\"http://i1.sinaimg.cn/IT/U5311P2T1D5539462F2755DT20110518095231.jpg\" width=\"135\" height=\"85\" alt=\"徕卡昂贵镜头遭遇切片\" /></a><h3><a href=\"http://tech.sina.com.cn/digi/dc/2011-05-18/09425539462.shtml\" target=\"_blank\">徕卡昂贵镜头遭遇切片</a></h3><p>最近,国外某大学的学生为了自己的毕业设计,...</p> </div>";
string patterns = @"(href|HREF|src|SRC|<p>)={1,}([""'^#][\w\S]*[""'>|</p>])";
MatchCollection matches = Regex.Matches(inputs, patterns);
foreach (Match match in matches)
{
Console.WriteLine("type: {0}", match.Groups[1].Value);
Console.WriteLine("href: {0}", match.Groups[2].Value);
Console.WriteLine("title: {0}", match.Groups[3].Value);
Console.WriteLine("Content: {0}", match.Groups[4].Value);
Console.WriteLine();
}