Vbscript 正则表达式实例及在qtp中应用
一、实例:
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
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.","IS1 is2 IS3 is4"))
运行结果:
二、在QTP中应用实例:
这里对WinList控件的操作使用的是Select方法,但是需要指定一长串的字符串,例如下代码:
Function SelectRegExp(Obj,Patrn,Button,Offset)
End Function
利用”RegisterUserFunc“把SelectRegExp函数注册注册到WinList控件的Select方法后,就可以使用重用后的方法,如以下代码:
'重写WinList控件的Select方法
RegisterUserFunc"WinList","select","SelectRegExp"
'使用重写后的WinList控件的Select方法
Window("Flight Reservation").Dialog("Flights Table").Activate
Window("Flight Reservation").Dialog("Flights Table").WinList("From").select "15797.*"
完整的代码如下:
Function SelectRegExp(Obj,oldFilter
End Function
Dialog("Login").WinEdit("Password:").Set "Cheers.lee"
Dialog("Login").WinEdit("Edit").SetSecure "48d51f921459c0234cb35e05
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").WinObject("Flight No:").Type "092208"
Window("Flight Reservation").WinComboBox("Departure Time:").Select "London"
Window("Flight Reservation").WinComboBox("Arrival Time:").Select "Los Angeles"
Window("Flight Reservation").WinCheckBox("text:=FLIGHT").click
RegisterUserFunc"WinList","SelectRegExp"
Window("Flight Reservation").Dialog("Flights Table").Activate
Window("Flight Reservation").Dialog("Flights Table").WinList("ListBox").select "19170.*"
'Window("Flight Reservation").Dialog("Flights Table").WinList("ListBox").Select "19170
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Edit").Set "lihuichang"
Window("Flight Reservation").WinRadioButton("Business").Set
Window("Flight Reservation").WinButton("Insert Order").Click
Window("Flight Reservation").Close
备注:推荐一个正则表达式学习的网址: