QTP VBScript RegExp对象的运用

发表于:2014-4-08 11:37

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:叶城宇    来源:51Testing软件测试网采编

  下面来讲讲RegExp对象:
  Vbs提供了针对正则表达式的一个非常实用的类,就是RegExp
Global属性:代表全局匹配
IgnoreCase属性:大小写忽略
Pattern属性:正则表达式
Execute方法:匹配搜索,返回匹配结果集合
Replace方法:匹配代替,返回替代匹配结果
Test方法:测试匹配,返回布尔类型
  下面举几个实例:
'判断正则匹配是否正确
'msgbox (IsRegMatch("a123","http://www.123.456.com"))
Function IsRegMatch(patrn,str)
Dim regEx
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = False
IsRegMatch = regEx.Test(str)
Set regEx = nothing
End Function
'替换匹配字符串
'msgbox (ReplaceRegMatch("9","loader runner 9.0, qtp 9.0","10"))
Function ReplaceRegMatch(patrn,str,replaceStr)
Dim regEx
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = False
regEx.Global = True   'false的时候只会替换第一个匹配的字符串。若为true则会替换所有匹配的字符串
ReplaceRegMatch = regEx.Replace(str,replaceStr)
End Function
'返回匹配内容
'returnRegMatch "qtp .","qtp 1 qtp 2 qtp3 qtp 4"
Function ReturnRegMatch(patrn,str)
Dim regEx,matches,match
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = true
regEx.Global = true  '打开全局搜索
Set matches = regEx.Execute(str)
For Each match in matches
print cstr(match.firstIndex) + " " + match.value + " " + cstr(match.length)
Next
End Function
《2023软件测试行业现状调查报告》独家发布~

精彩评论

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号