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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值