欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

正則表達(dá)式提取網(wǎng)址、標(biāo)題、圖片等一例(.Net Asp Javascript/Js)的實(shí)現(xiàn)

 更新時(shí)間:2008年11月08日 19:01:06   作者:  
用各種語(yǔ)言實(shí)現(xiàn)的提取內(nèi)容中的網(wǎng)址,標(biāo)題,圖片等功能代碼,對(duì)于大家掌握正則的共用性有很大的幫助。
在一些抓取、過(guò)濾等情況下, 正則表達(dá)式 regular expression 的優(yōu)勢(shì)是很明顯的。
例如,有如下的字符串:
復(fù)制代碼 代碼如下:

<li><a title="FCKEditor高亮代碼插件測(cè)試"><span class="article-date">[09/11]</span>FCKEditor高亮代碼插件測(cè)試</a></li>

現(xiàn)在,需要提取 href 后面的網(wǎng)址,[]內(nèi)的日期,和 鏈接的文字。
下面給出C#, ASP 和 Javascript 的實(shí)現(xiàn)方式
C#的實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

string strHTML = "<li><a \"href=http://www.abcxyz.com/something/article/143.htm\" title=\"FCKEditor高亮代碼插件測(cè)試\"><span class=\"article-date\">[09/11]</span>FCKEditor高亮代碼插件測(cè)試</a></li>";
string pattern = "http://([^\\s]+)\".+?span.+?\\[(.+?)\\].+?>(.+?)<";
Regex reg = new Regex( pattern, RegexOptions.IgnoreCase );
MatchCollection mc = reg.Matches( strHTML );
if (mc.Count > 0)
{
foreach (Match m in mc)
{
Console.WriteLine( m.Groups[1].Value );
Console.WriteLine( m.Groups[2].Value );
Console.WriteLine( m.Groups[3].Value );
}
}

ASP的實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

<%
Dim str, reg, objMatches
str = "<li><a href=""http://localhost/Z-Blog18/article/143.htm"" title=""FCKEditor高亮代碼插件測(cè)試""><span class=""article-date"">[09/11]</span>FCKEditor高亮代碼插件測(cè)試</a></li>"
Set reg = new RegExp
reg.IgnoreCase = True
reg.Global = True
reg.Pattern = "http://([^\s]+)"".+?span.+?\[(.+?)\].+?>(.+?)<"
Set objMatches = reg.Execute(str)
If objMatches.Count > 0 Then
Response.Write("網(wǎng)址:")
Response.Write(objMatches(0).SubMatches(0))
Response.Write("<br>")
Response.Write("日期:")
Response.Write(objMatches(0).SubMatches(1))
Response.Write("<br>")
Response.Write("標(biāo)題:")
Response.Write(objMatches(0).SubMatches(2))
End If
%>

Javascript的實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

<script type="text/javascript">
var str = '<li><a href="http://localhost/Z-Blog18/article/143.htm" title="FCKEditor高亮代碼插件測(cè)試"><span class="article-date">[09/11]</span>FCKEditor高亮代碼插件測(cè)試</a></li>';
var pattern = /http:\/\/([^\s]+)".+?span.+?\[(.+?)\].+?>(.+?)</gi;
var mts = pattern.exec(str);
if (mts != null)
{
alert(mts[1]);
alert(mts[2]);
alert(mts[3]);
alert(mts[4]);
}
</script>

相關(guān)文章

最新評(píng)論