首页 新闻 会员 周边

求一正则表达式

0
悬赏园豆:30 [已解决问题] 解决于 2015-12-02 15:44
<li class="cat_list_con_1"><span class="cat_span1"><input type="checkbox" name="contentid[]" value="8116830"/><a href="show.php?contentid=8116830" target="_blank"><img src="http://image.abc.com/uploadfile/T/MAE830.jpg" alt="组合工具,世达6.3 X 10 X 12.5mm系列120件套 09014" width="50" height="50" onmouseover="showpic(this,'http://image.abc.com/uploadfile/T/MAE830.jpg','showimg8116830');" onmouseout="judge('8116830')" /></a><span class="hiddenshow" onmouseout="hiddenpic('showimg8116830')" id="showimg8116830"><img width="100" height="100" border="0" alt="组合工具,世达6.3 X 10 X 12.5mm系列120件套 09014" src="http://image.abc.com/uploadfile/T/MAE830.jpg" id="showimg_8116830"></span></span><span class="cat_span2"><a href="show.php?contentid=8116830" title="组合工具,世达6.3 X 10 X 12.5mm系列120件套 09014 " target="_blank">组合工具,世达6.3 X 10 X 12.5mm系列120件套 09014 </a><br /><strong>订货号:<a href="show.php?contentid=8116830" title="MAE830" target="_blank">MAE830</a></strong></span></li>

已知如上HTML,特征是class="cat_list_con_1",现在要取出show.php?contentid=8116830这段内容,正则怎么写呢?

happydaily的主页 happydaily | 菜鸟二级 | 园豆:301
提问于:2015-12-02 14:03
< >
分享
最佳答案
0
string pattern = "<li class=\"cat_list_con_1\">.*href=\"(.*?)\".*</li>";
string match = Regex.Match(data, pattern).Groups[1].Value;
收获园豆:20
jio92 | 小虾三级 |园豆:1064 | 2015-12-02 14:40

data是你的html

jio92 | 园豆:1064 (小虾三级) | 2015-12-02 14:40

@jio92: 要取出这段代码的show.php?contentid=8116830内容,注意contentid=这儿数字可能不一样

happydaily | 园豆:301 (菜鸟二级) | 2015-12-02 14:53

@happydaily: 嗯,你的前提是class="cat_list_con_1",也就是你这里3个href都在<li>中,有这个条件后就没法循环匹配,因为你只有1个class="cat_list_con_1",然后这个里面有3个href,还有也没法定取第几个href的值, 上面的匹配会拿到最后一个href的值.  

要么你就全部取出来

string pattern = "<li class=\"cat_list_con_1\">.*href=\"(.*?)\".*href=\"(.*?)\".*href=\"(.*?)\".*</li>";
GroupCollection groups = Regex.Match(data, pattern).Groups;
for (int i = 0; i < groups.Count; i++)
{
     Console.WriteLine(groups[i].Value);
}

groups[0].Value是原字符串,后面开始才是匹配的值

jio92 | 园豆:1064 (小虾三级) | 2015-12-02 15:10

@jio92: 用正则的取数据的前提是 要找好 起点->终点  对于你这个class条件  起点就是<li class.....>  终点就是</li>

jio92 | 园豆:1064 (小虾三级) | 2015-12-02 15:15

@jio92: 我要用Regex.Matches,有很多组格式相同的Li

happydaily | 园豆:301 (菜鸟二级) | 2015-12-02 15:43
其他回答(1)
1

给你介绍一个很不错的开源项目。直接可以将html代码转换成xml格式的文件,直接操作xml。比使用正则表达式方便很多。

http://htmlagilitypack.codeplex.com/releases/view/90925 dll下载地址

将你上面的html代码复制到d盘新建html.txt中。

public static void Main(string[] args)
        {
            //将html代码放到d:\html.txt
            StreamReader sr = new StreamReader(@"d:\html.txt");
            //获取html代码
            string htmlStr = sr.ReadToEnd();
            Console.WriteLine(htmlStr);
            //转换为数组放入到读流中
            byte[] htmlArray = Encoding.ASCII.GetBytes(htmlStr);
            MemoryStream memoryStream = new MemoryStream(htmlArray);
            StreamReader readerHtml = new StreamReader(memoryStream);
            //初始化插件
            HtmlDocument htmlDoc = new HtmlDocument();
            //加载html成xml
            htmlDoc.Load(readerHtml);
            HtmlNodeCollection cat_list_con_1 = htmlDoc.DocumentNode.SelectNodes("//li[@class='cat_list_con_1']");
            HtmlNode aNode = cat_list_con_1[0].FirstChild.ChildNodes[1];
            Console.WriteLine();
            Console.WriteLine(aNode.GetAttributeValue("href",""));
}

收获园豆:10
凝冰 | 园豆:685 (小虾三级) | 2015-12-02 14:35

感谢,这也是个不错的解决方案

支持(0) 反对(0) happydaily | 园豆:301 (菜鸟二级) | 2015-12-02 15:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册