VBScript中使用正则表达式

上一篇 / 下一篇  2010-06-12 11:24:51 / 个人分类:VB脚本

在VBScript中,使用RegExp对象来支持正则表达式的使用,RegExp提供了三个属性和三个方法:
属性:Pattern、Global、IgnoreCase
方法:Test、Replace、Execute
1、Pattern:用于描述正则表达式的匹配模式。
     例如,用+匹配前面的表达式一次或多次,用.匹配除换行符\n之外的任何单字符。
下面使用Pattern为is,用于匹配字符串“IS1 is2 IS3 is4”。
Function RegExpTest(patrn,strng)
 Dim regEx,Match,Matches
Set regEx = New RegExp  '创建一个正则表达式
regEx.Pattern  = patrn '设置正则表达式的匹配模式
regEx.IgnoreCase = True '设置为不区分大小写
regEx.Global = True '设置为全局可用
Set Matches = regEx.Execute(strng) '执行搜索
For Each Match in Matches '便利所有匹配项。
 RetStr = RetStr &"在"
 RetStr = RetStr&Match.FirstIndex&"找到匹配项,匹配项的值为"‘
 Retstr = RetStr&Match.Value &"."&vbCRLF
Next
RegExpTest = RetStr
End Function

'调用RegExpTest
msgBox(RetExpTest("is.","IS1 is2 IS3 is4"))
 

TAG:

 

评分:0

我来说两句

日历

« 2024-04-29  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 9273
  • 日志数: 22
  • 建立时间: 2010-01-16
  • 更新时间: 2010-11-08

RSS订阅

Open Toolbar