|
原帖由 tjmjf 于 2009-7-10 11:14 发表
在网上搜索了很多QTP关于正则表达式的东西,但是大多都是讲那些理论,没有实际的东西。
假设,x="abcd",y="a",z="b" 我想用正则表达式达到这样的目的:
1.验证x是不是以“a”开头;
2.验证x是不是包含“b”
最好 ...
QTP的帮助文档能回答你这两个问题
1. 写正则表达式
可以参见帮助文档中的HP QuickTest Professional User's Guide > Enhancing Tests > Configuring Values > Defining Regular Expressions
比如
Matching the Beginning of a Line A caret (^) instructs QuickTest to match the expression only at the start of a line, or after a newline character.
For example:
book
matches book within the lines—book, my book, and book list, while
^book
matches book only in the lines—book and book list.
2. 验证正则表达式
可以参考帮助中的这段描述和代码
Remarks
The actual pattern for the regular expression search is set using the Pattern property of the RegExp object.
The Execute method returns a Matches collection containing a Match object for each match found in string. Execute returns an empty Matches collection if no match is found.
The following code illustrates the use of the Execute method.
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches ' Create variable.
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
Set Matches = regEx.Execute(strng) ' Execute search.
For Each Match in Matches ' Iterate Matches collection.
RetStr = RetStr & "Match found at position "
RetStr = RetStr & Match.FirstIndex & ". Match Value is '"
RetStr = RetStr & Match.Value & "'." & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))
[ 本帖最后由 hsjzfling 于 2009-7-10 14:45 编辑 ] |
|