您的位置:程序门 -> .net技术 -> c#



如何写正则式匹配css的引用链接?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何写正则式匹配css的引用链接?[已结贴,结贴人:live41]
发表于:2007-02-08 11:12:18 楼主
例如下面这段js代码,怎么用正则从其中找出http://ads.sina.com/cnnews/css/cnnews.css这个链接?

browser   =   navigator.appname;
ie   =   "microsoft   internet   explorer ";
netscape   =   "netscape ";
os   =   navigator.platform;
mac   =   'macppc '

if   (browser   ==   netscape   &&   os   !=   mac)   {
                document.write( ' <link   rel= "stylesheet "   type= "text/css "   href= "http://ads.sina.com/cnnews/css/cnnews.css "   title= "master "> ');
                }
else   if   (browser   ==   ie   &&   os   !=   mac)   {
                document.write( ' <link   rel= "stylesheet "   type= "text/css "   href= "http://ads.sina.com/cnnews/css/cnnews.css "   title= "master "> ');
                }
else   if   (browser   ==   netscape   &&   os   ==   mac)   {
                document.write( ' <link   rel= "stylesheet "   type= "text/css "   href= "http://ads.sina.com/cnnews/css/cnnews.css "   title= "master "> ');
                }
else   if   (browser   ==   ie   &&   os   ==   mac)   {
                document.write( ' <link   rel= "stylesheet "   type= "text/css "   href= "http://ads.sina.com/cnnews/css/cnnews.css "   title= "master "> ');
                }
发表于:2007-02-08 13:03:581楼 得分:0
该回复于2007-12-21 20:21:41被管理员或版主删除
发表于:2007-02-08 13:15:592楼 得分:36
试试
string   pattern   =   @ "http://.*?\.css ";
regex   regex   =   new   regex(pattern,   regexoptions.singleline);
foreach   (match   m   in   regex.matches(str))
{
        messagebox.show(m.value);
}
发表于:2007-02-08 15:40:023楼 得分:64
string   yourstr   =   ......;
matchcollection   mc   =   regex.matches(yourstr,   " <link\\s+rel=\ "stylesheet\ "\\s+type=\ "text/css\ "\\s+href=\ "(? <url> .+?)\ ".+?> ",   regexoptions.ignorecase);
foreach(match   m   in   mc)
{
        m.groups[ "url "].value;//http://ads.sina.com/cnnews/css/cnnews.css
}
发表于:2007-02-09 09:47:354楼 得分:0
谢谢你们两位高手。。。

我用了逐字扫描判断来实现了,不过仍然很感谢两位!


快速检索

热门点击