测试共享,雁过留痕

QTP中如何使用正则表达式

上一篇 / 下一篇  2008-07-31 17:07:45 / 个人分类:QTP

1.如何使用正则表达式
v Z8H&e:VR D lY"?0首先生成正则表达式对象
X:UaE(? X)mR0Set regEx = New RegExp
t!s4vHau\ B)e@8@051Testing软件测试网}F-FE!TM)m~
然后设置正则表达式对象的属性
"v)@s!Vokq0regEx.Pattern = patrn51Testing软件测试网$W{$}O} B.U8T
regEx.IgnoreCase = True
Ka G$Mk|6iLi\t ~0regEx.Global = True
)Q ]9]qQ"zC0
pri V/_-`Bf0最后使用正则表达式的方法来实现需求51Testing软件测试网!Z)s%F@'Q
regEx.Replace(string1, string2)51Testing软件测试网M$_H;e7K cTP
regEx.Test(string)51Testing软件测试网GRU r @gR+|
regEx.Execute(string)51Testing软件测试网2ry.QAk;HU t
51Testing软件测试网%h;rx VB*Sm^d!n
2.正则表达式对象的属性与方法:51Testing软件测试网.XVek W*V%m
属性:51Testing软件测试网g"yvgT'q fu
Global Property51Testing软件测试网,ZE5pZ^
IgnoreCase Property51Testing软件测试网 u8r|6t{!qD a
Pattern Property51Testing软件测试网uir!Y@ oqV

F^#U2G)A)w$zN7c0方法:
Ro,Mb a fqFT0Execute
$L)B]!{0L+} m\0Preplace51Testing软件测试网!^_u%Wp
Test51Testing软件测试网,n"Oo3]C^:J3B0m
51Testing软件测试网GzP*H-S6A5E P
3.实例:简单的字母搜索,test方法
} f'UQF%D&Mia P0'定义变量及初始化51Testing软件测试网5rl/~+JtI
Dim a,b,c
%f"x {!z8XM0a="mooooon"
l*^!F)\I0g/Vnm0b="mo*n"
)`s)ZS(X(w0
qlg;Z4H qJ#`0Z0'正则表达式以及执行搜索51Testing软件测试网A~C@F%J t
Set myReg= New RegExp '生成正则表达式对象51Testing软件测试网My]5lRb)~n
myReg.Pattern =b             '设置正则表达式公式(必须有本语句)51Testing软件测试网9S2|H-\'oK+|2Z
myReg.IgnoreCase = True '表示匹配时忽略大小写(非必须语句)51Testing软件测试网,ui.K7jr3g1\.O.?b3{E
myReg.Global = True '非必须语句
S y)~@Xy0Cj0
5b1U.h ezB { n0c=myReg.test(a) '执行搜索,如果在a中搜到a,则返回true,否则faulse。51Testing软件测试网fyB\9Vrlj9W
'报告执行结果51Testing软件测试网.Vu)D m%TZo,j
If c Then51Testing软件测试网7l"c v)}j"`
Reporter.ReportEvent 2, "测试结果", "a中包括b"
5t+H0b"e"hln0else
1t4T!m(Fk0Reporter.ReportEvent 1, "测试结果", "a中不包括 b"
? v*w F+{Zz0End If

TAG: QTP

 

评分:0

我来说两句

Open Toolbar