ASP 正则表达式匹配字符串提取

ASP 正则表达式,网上搜到的文章都是转载的同一份,且只说明了替换,没有说明怎么提取需要的字符串,这里给出一个例子.

起因是一个CSDN网友问起怎么提取meta的字符集,自己便琢磨了一下.

几个要点:

1.Matches对象 是否匹配到

使用Count属性,若>0则表示匹配到了

2.SubMatches 提取要匹配字符串的关键属性,为一数组

若提取的部分(挎号所挎的部分)为一个,则取索引0,例如:

 matches(0).SubMatches(0)

若取多个,则索引依此类推.

3. ASP中双引号转义使用两个连续的双引号"",和数据库查询中的单引号转义有点类似,例如:

要定义下面的表达式:

<meta charset="UTF-8"/>

使用转义后:

str = "<meta charset=""UTF-8"" />"

<script language="vbscript">
Dim str, re, rv
str = "<meta http-equiv=Content-Type content=text/html;charset=gb2312>"
str = "<meta http-equiv=Content-Type content=""text/html;charset=gb2312"">"
str = "<meta http-equiv=Content-Type content=""text/html;charset=gb2312""/>"
str = "<meta http-equiv=Content-Type content=‘text/html;charset=gb2312’/>"
str = "<meta charset=""UTF-8"" />"

Set re = New RegExp
re.Pattern = "<meta[^>]+charset=[""]?([\w\-]+)[^>]*>"
re.Global = True
re.IgnoreCase = True
re.MultiLine = True

Set matches = re.Execute(str)

if matches.Count>0 then
	msgbox matches(0).SubMatches(0)
end if

</script>


ASP 正则表达式参考:

http://vod.sjtu.edu.cn/help/Article_Show.asp?ArticleID=2124&ArticlePage=2

posted @ 2011-08-23 12:34  hongweigg  阅读(33)  评论(0)    收藏  举报