在一些抓取、过滤等情况下, 正则表达式 regular expression 的优势是很明显的。
例如,有如下的字符串:
复制代码代码如下:
<li><a href="http://www.abcxyz.com/something/article/143.htm" style="border-left-color: rgb(0, 153, 204); border-left-width: 1px; border-left-style: solid; padding: 0px 3px; margin: 3px auto 0px; width: 640px; background-color: rgb(242, 246, 251); clear: both; border-top-color: rgb(0, 153, 204); border-top-width: 1px; border-top-style: solid; border-right-color: rgb(0, 153, 204); border-right-width: 1px; border-right-style: solid;"> 复制代码代码如下:
string strHTML = "<li><a /"href=http://www.abcxyz.com/something/article/143.htm/" style="border-left-color: rgb(0, 153, 204); border-left-width: 1px; border-left-style: solid; padding: 0px 3px; margin: 3px auto 0px; width: 640px; background-color: rgb(242, 246, 251); clear: both; border-top-color: rgb(0, 153, 204); border-top-width: 1px; border-top-style: solid; border-right-color: rgb(0, 153, 204); border-right-width: 1px; border-right-style: solid;"> 复制代码代码如下:
<%
Dim str, reg, objMatches
str = "<li><a href=""http://localhost/Z-Blog18/article/143.htm"" style="border-left-color: rgb(0, 153, 204); border-left-width: 1px; border-left-style: solid; padding: 0px 3px; margin: 3px auto 0px; width: 640px; background-color: rgb(242, 246, 251); clear: both; border-top-color: rgb(0, 153, 204); border-top-width: 1px; border-top-style: solid; border-right-color: rgb(0, 153, 204); border-right-width: 1px; border-right-style: solid;"> 复制代码代码如下:
<script type="text/javascript">
var str = '<li><a href="http://localhost/Z-Blog18/article/143.htm" title="FCKEditor高亮代码插件测试"><span class="article-date">[09/11]</span>FCKEditor高亮代码插件测试</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>