regular expression

第十三章

正则表达式就是用一组匹配某种模式的特殊字符串组成的一个公式。使用这个特征可以用于颜色的高亮显示及其他的一些操作。本章涵盖了正则表达式的基本知识以及如何在QTP中使用它们。图13-1总结了常用的元字符:

                           图13.1

元字符

说  明

.

匹配任何单个字符

[xyz]

匹配括号中的任何一个字符

[^xyz]

匹配除了括号中的任何一个字符

[x-y]

匹配括号中字符区间内的任何一个字符

*

匹配0或多个正好在它之前的那个字符

+

匹配1或多个正好在它之前的那个字符

?

匹配0或1个正好在它之前的那个字符

()

将(和)之间的表达式定义为“组”

|

将多个匹配条件进行逻辑“或”运算

^

匹配输入字符串的开始位置

$

匹配输入字符串的结束位置

\w

匹配任何字母数字字符和下划线

\W

匹配任何非字母数字字符

\

用来将这里列出的这些元字符当作普通的字符来进行匹配

{}

匹配指定数目的字符

正则表达式特征

匹配任何单个字符(.

.匹配任何单个字符。例如正则表达式“”匹配“12B”,“B3B”,“123”,“ABC”,“$g%”等。

匹配括号中的任何一个字符([xyz])

匹配方括号中的任何一个字符。例如正则表达式“[abc][12]”匹配“a1”,“a2”,“b1”,“b2”,“c1”和“c2”。

匹配除了括号中的任何一个字符([^xyz])

这里的”^” 用法是排除,匹配除了指定区间之外的字符。例如“[^a][12]”与“a1”和“a2”不匹配,但是可以匹配“11”,“12”,“c1”等。

匹配括号中字符区间内的任何一个字符([x-y])

[d-h]结构的正则表达式将匹配在dp之间的任何一个字符。例如“[abcdef][123]”可以写出“[a-f][1-3]

匹配0或多个正好在它之前的那个字符(*)

*”用于匹配0或多个正好在它之前的那个字符。例如a*”可以匹配“a”,“aa”,“aaa”等,正则表达式.*意味着能够匹配任意数量的任何字符。请看另外的一些例子:

aa*”可匹配“a”,“aa”,“aaa”等;

[abc][1-4]*”可匹配“a”,“b”,“c”,“a1”,“b1”,“a12”,“a1234344”等;

User.*”可匹配任何以“User”开头的字符串。

*******************************************************************************

注意:很多人会有这样的错误,用“User*”匹配以“User”开头的字符串,而“User*”只匹配像“Use”,“User”,“Userr”等

*******************************************************************************

匹配1或多个正好在它之前的那个字符(+)

+”用于 匹配1或多个正好在它之前的那个字符。例如“aa*”可以等同于“a+”。

123+”可匹配“123”,“1233”,“1233”等;

[123]+”可匹配“1”,“2”,“3”,“12”,“23”等

匹配0或1个正好在它之前的那个字符(?)

?”用于匹配01个正好在它之前的那个字符。例如“a[123]?”可匹配“a”,“a1”,“a2”和“a3”。

正则表达式分组(())

用括号“()”组成一个正则表达式。例如(123)+匹配“123”,“123123”,“123123123”等,与“123+”的含义是不一样的。

匹配多个表达式中的一个(|

|”符号是指进行逻辑“或”运算。例如“(123)|(abc)”可以匹配“123”或“abc”。

匹配输入字符串的开始位置(^)

^符号在正则表达式的开头用于匹配一行的开始。例如“^abc.*”能够匹配以“abc”开始的字符串。

匹配输入字符串的结束位置($)

$”符号在正则表达式的末尾用于匹配一行的结束。例如“abc$”能够匹配“abc”,“123abc”,“aabc”等,而“^abc$”只能匹配“abc”。

匹配任何字母数字字符和下划线(\w)

\w”可匹配任何字母数字和下划线。这个用法与表达式“[A-Za-z0-9_]”是一样的。例如“\w\w”能够匹配“A_”,“a1”等。

匹配任何非字母数字字符(\W)

\W”可匹配任何非字母和非数字的字符,是与“\w”相反的,它可以等同于“[^A-Za-z0-9]”。例如“\W”可以匹配“$”,“%”,“@”等。

转义字符(\

斜杠将下一个字符标记为一个原意字符或一个特殊字符。例如“.”可以匹配任意字符,而我们用“\.”来匹配点字符。通常“t”被视为普通的原义字符,但在其前面放置一个斜杠后就被当成制表符“\t”。

匹配指定数目的字符({})

{}”匹配X个正好在它之前的那个字符。使用这个符号有多种方法,见表13-2

13-2 

字符

说明

举例

{M}

M个匹配的模式

A{2}仅可以匹配“AA

{M,}

M个或更多个匹配的模式

A{1,}可以匹配“A”,“AA”等,这个表达式等同于“A+”,“A{0}”等同于“A*

{M,N}

至少M个且至多N个匹配的模式

A{1,3}可以匹配“A”,“AA”和“AAA

其他正则表达式字符

虽然在QTP的用户手册中没有列举所有的正则表达式字符,但是它支持所有的VBScript正则表达式字符。下面表13-3中列出了其他的一些正则表达式符号。

13-3 

字符

说明

举例

\d

匹配09的任意数字,等同于“[0-9].

\d{2}”可以匹配0199的所有数字,但是它不匹配19的单数

\D

匹配任意的非数字字符,等同于“[^0-9].

\D”可以匹配“A”,“&”,“$”等

\num

匹配num,其中num是一个正整数,对所获取的匹配的引用 

“(\d)\1\1”可以匹配”111”,”222”,”333”

\s

匹配任何空白字符,等同于“[\f\n\r\t\v]

\S

匹配任何非空白字符,等同于“[^\f\n\r\t\v]

问题13.1:能够匹配如下三个字符串的正则表达式是什么?“test.app.com”,”qa.app.com”,”www.app.com

在这三组字符串中可以看到都含有“.app.com”,所以我们可以用“\.app\.com”来匹配它(在“.”之前使用了符号“\”,所以这里的“.”是作为一个普通字符而不是元字符)。现在我们需要一个正则表达式,能够表示“test”“qa”“www”中的一个字符串,这个表达式是(test|qa|www)。那么这个完整的正则表达式就是“(test|qa|www)\.app\.com

问题13.2 用以匹配日期型MM/DD/YY的正则表达式如何写?

MM”可以是112的数字,其正则表达式是“[1-9]|1[0-2]”。“DD”可以是131的数字,其正则表达式是“[1-9]|[1-2][0-9]|3[0-1]”。“YY”的正则表达式则是“[0-9][0-9]”。组合起来日期型的正则表达式就是“([1-9]|1[0-2])/( [1-9]|[1-2][0-9]|3[0-1])/([0-9][0-9])”。

这其实是匹配一个含有日期的字符串,如果我们让它只匹配日期型字符串,那么可以在其前面用“^”在结束处用“$”。最终这个正则表达式就是“^([1-9]|1[0-2])/([1-9]|[1-2][0-9]|3[0-1])/[0-9][0-9]$”。

QTP中何时使用正则表达式?

如果一个预期的属性的值是动态的,但又遵循一定的模式,这种情况使用正则表达式将是最佳解决方案。例如,NameID_233NameID_256NameID_290…等等,它们遵循一个模式,都是以字符串“NameID_”开始,然后跟一个数字。这种模式可以用正则表达式“NameID_.*”来表示,这里的“.”和“*”是元字符,其意义在表13-1中已解释。符号“.”是指任何字符,跟在“.”之后的符号“*”是指0个或多个其前面的那个字符。

正则表达式也可以用来识别QTP测试对象的属性、动态值或者是有许多类似模式的情况。我们以一个网页文本框为例,所有的名称均以“Name”开头,而实际名称的模式是“Name1”、“Name2”、“Name3…等。访问这些文本框对象,我们可以用描述性编程(DP)和下面的方法。

方法一

'Create a description object for text box

Set oTextBox = Description.Create

'the regular expression to match a value starting Name

oTextBox("name").value = "name.*"

'This property by default is True,so we could skip this line of code 

oTextBox("name").RegularExpression = True

'To access the first text box whose name start with Name

oTextBox("index").value = 0

'Set the value of the text box 

Browser("").Page("").WebEdit(oTextBox).Set "Value"

方法二

‘Set the value of first text box whose name start with “Name”

Browser(“”).Page(“”).WebEdit (“name:=Name.*”,”index:=θ”).Set “Value”

In this method we can also avoid using the regular expression by generating the name at runtime as given here.

 ‘The text box to be set 

   i=1

‘Set the text box value

Browser(“”).Page(“”).WebEdit (“name:=Name” & i) .Set “Value”

如果我们的对象是放在对象库中的,那么需要在“Object Repository Manager.”中更改它的值,打开工具栏上的“Object Repository”并浏览对象,如图13-1所示。点击“Constant value options”按钮,点击“Regular Expression”复选框,在输入框中输入相应的内容,如图13-2所示。

-----------------------------------------------------------------------------------------------------------

注意:在输入文本内容后点击“Regular expression”,QTP会问我们是否要在所有的特殊字符前加“\”,如果我们想要它匹配的是按照字面意思的字符串就选择是,如果想要将它作为一种模式则选择否。

-----------------------------------------------------------------------------------------------------------

如果我们不知道字面正则表达式是什么,例如网页地址“http://www.testapp.com/Test%20Test.html”,我们可以在文本框中输入此网页地址,点击“Regular expression”复选框,然后会打开一个对话框询问是否在特殊字符前加“\”,如图13.3所示,在点击“是”之后,生成的字面正则表达式是“http://www\.testapp\.com/Test%20Test\.html

使用代码测试正则表达式

QTP通过微软VBScript引擎,提供RegExp,用于一般的正则表达式赋值和处理。下面是一个函数,用于测试匹配字符串中满足匹配要求的模式:

'Function to check if a string matches a regular expression pattern

Function IsRegEqual(s_Text, s_Pattern, MatchCompleteString)

    Dim regEx, retVal 

    

    ‘ Create regular expression object.

    Set regEx = New RegExp 

    

    ‘Set pattern.

    regEx.Pattern = s_Pattern 

    'Ignore case

    regEx.IgnoreCase = True

    'Match complete string or not

    regEx.Global = MatchCompleteString 

    

    ‘Test the regular expression

    IsRegEqual = regEx.Test(s_Text) 

    Set regEx = Nothing

End Function

Usage:

‘Will display True

Msgbox IsRegEqual(“foos”,”foo|bar”, False)

‘Will display False

Msgbox IsRegEqual(“foos”,”foo|bar”, True)

‘Will display False

Msgbox IsRegEqual(“foos”,”fo(o|b)ar”, True)

‘Will display True

Msgbox IsRegEqual(“Are you foo?”,”foo”,True)

‘Will display False . This is for a string starting and ending with foo which is foo only.

Msgbox IsRegEqual (“Are you foo?”,”^foo$”,True) 

使用正则表达式来选择WebListWebRadioGroup的值

要从WebListWebRadioGroup中选择一个值,首先我们要使用“all items”获取到对象中包含的所有项目的列表,返回的值用分号“;”隔开,然后我们使用split函数把所有的值放在数组中:

'Function for selecting a value based on regular expression

'matchIndex is a 0 based value

Function WebRegSelectValue(ByVal oObject, ByVal Pattern, ByVal matchIndex)

Dim allItemValues, matches

matches = 0

'Get all the items into an array

allItemValues = Split(ReplaceWhiteCha(oObject.GetROProperty("all items"))," ")

'Loop through all the values in the array

For i = LBound(allItemValues) to UBound(allItemValues)

'See if the current value matches the pattern

If IsRegEqual(allItemValues(i), Pattern, True) Then

'If match found is equal to the no. of matched then 

'select the value

If matches = matchIndex Then

oObject.Select i

Exit Function

End If

matches = matches + 1

End If

Next

'No matching value found. Raise error

Err.Raise vbObjectError + 1, "WebRegSelectValue","Could not find the specified value - " & Pattern & ", Index - " & matchIndex

End Function

Function IsRegEqual(s_Text, s_Pattern, MatchCompleteString)

    Dim regEx, retVal 

    

    ' Create regular expression object.

    Set regEx = New RegExp 

    

    'Set pattern.

    regEx.Pattern = s_Pattern 

    'Ignore case

    regEx.IgnoreCase = True

    'Match complete string or not

    regEx.Global = MatchCompleteString 

    

    'Test the regular expression

    IsRegEqual = regEx.Test(s_Text) 

    Set regEx = Nothing

End Function

Public Function ReplaceWhiteCha(ByVal Text)

'String pattern ending with carriage return, tab, new line

'or a space character

LTrimPattern = "[\s]"

Dim oRegExp

Set oRegExp = New RegExp

oRegExp.Pattern = LTrimPattern

oRegExp.Global = True

ReplaceWhiteCha = oRegExp.Replace(Text," ")

End Function

'Register the new function

RegisterUserFunc "WinComboBox","SelectReg","WebRegSelectValue"

Window("Flight Reservation").WinComboBox("Fly From:").SelectReg  "Frankfurt",0

使用正则表达式提取信息

正则表达式也被用来从字符串中提取信息,考虑“Your package will be shipped on 10/1/2008 and will be delivered by 15/1/2008”,现在我们用正则表达式匹配日期型模式“DD/MM/YYYY”即是“(0[1-9]|[12][0-9]|3[0-1])/(0?[1-9]|1[0-2])/([0-9]{4})

第十三章

正则表达式就是用一组匹配某种模式的特殊字符串组成的一个公式。使用这个特征可以用于颜色的高亮显示及其他的一些操作。本章涵盖了正则表达式的基本知识以及如何在QTP中使用它们。图13-1总结了常用的元字符:

                           图13.1

元字符

说  明

.

匹配任何单个字符

[xyz]

匹配括号中的任何一个字符

[^xyz]

匹配除了括号中的任何一个字符

[x-y]

匹配括号中字符区间内的任何一个字符

*

匹配0或多个正好在它之前的那个字符

+

匹配1或多个正好在它之前的那个字符

?

匹配0或1个正好在它之前的那个字符

()

将(和)之间的表达式定义为“组”

|

将多个匹配条件进行逻辑“或”运算

^

匹配输入字符串的开始位置

$

匹配输入字符串的结束位置

\w

匹配任何字母数字字符和下划线

\W

匹配任何非字母数字字符

\

用来将这里列出的这些元字符当作普通的字符来进行匹配

{}

匹配指定数目的字符

正则表达式特征

匹配任何单个字符(.

.匹配任何单个字符。例如正则表达式“”匹配“12B”,“B3B”,“123”,“ABC”,“$g%”等。

匹配括号中的任何一个字符([xyz])

匹配方括号中的任何一个字符。例如正则表达式“[abc][12]”匹配“a1”,“a2”,“b1”,“b2”,“c1”和“c2”。

匹配除了括号中的任何一个字符([^xyz])

这里的”^” 用法是排除,匹配除了指定区间之外的字符。例如“[^a][12]”与“a1”和“a2”不匹配,但是可以匹配“11”,“12”,“c1”等。

匹配括号中字符区间内的任何一个字符([x-y])

[d-h]结构的正则表达式将匹配在dp之间的任何一个字符。例如“[abcdef][123]”可以写出“[a-f][1-3]

匹配0或多个正好在它之前的那个字符(*)

*”用于匹配0或多个正好在它之前的那个字符。例如a*”可以匹配“a”,“aa”,“aaa”等,正则表达式.*意味着能够匹配任意数量的任何字符。请看另外的一些例子:

aa*”可匹配“a”,“aa”,“aaa”等;

[abc][1-4]*”可匹配“a”,“b”,“c”,“a1”,“b1”,“a12”,“a1234344”等;

User.*”可匹配任何以“User”开头的字符串。

*******************************************************************************

注意:很多人会有这样的错误,用“User*”匹配以“User”开头的字符串,而“User*”只匹配像“Use”,“User”,“Userr”等

*******************************************************************************

匹配1或多个正好在它之前的那个字符(+)

+”用于 匹配1或多个正好在它之前的那个字符。例如“aa*”可以等同于“a+”。

123+”可匹配“123”,“1233”,“1233”等;

[123]+”可匹配“1”,“2”,“3”,“12”,“23”等

匹配0或1个正好在它之前的那个字符(?)

?”用于匹配01个正好在它之前的那个字符。例如“a[123]?”可匹配“a”,“a1”,“a2”和“a3”。

正则表达式分组(())

用括号“()”组成一个正则表达式。例如(123)+匹配“123”,“123123”,“123123123”等,与“123+”的含义是不一样的。

匹配多个表达式中的一个(|

|”符号是指进行逻辑“或”运算。例如“(123)|(abc)”可以匹配“123”或“abc”。

匹配输入字符串的开始位置(^)

^符号在正则表达式的开头用于匹配一行的开始。例如“^abc.*”能够匹配以“abc”开始的字符串。

匹配输入字符串的结束位置($)

$”符号在正则表达式的末尾用于匹配一行的结束。例如“abc$”能够匹配“abc”,“123abc”,“aabc”等,而“^abc$”只能匹配“abc”。

匹配任何字母数字字符和下划线(\w)

\w”可匹配任何字母数字和下划线。这个用法与表达式“[A-Za-z0-9_]”是一样的。例如“\w\w”能够匹配“A_”,“a1”等。

匹配任何非字母数字字符(\W)

\W”可匹配任何非字母和非数字的字符,是与“\w”相反的,它可以等同于“[^A-Za-z0-9]”。例如“\W”可以匹配“$”,“%”,“@”等。

转义字符(\

斜杠将下一个字符标记为一个原意字符或一个特殊字符。例如“.”可以匹配任意字符,而我们用“\.”来匹配点字符。通常“t”被视为普通的原义字符,但在其前面放置一个斜杠后就被当成制表符“\t”。

匹配指定数目的字符({})

{}”匹配X个正好在它之前的那个字符。使用这个符号有多种方法,见表13-2

13-2 

字符

说明

举例

{M}

M个匹配的模式

A{2}仅可以匹配“AA

{M,}

M个或更多个匹配的模式

A{1,}可以匹配“A”,“AA”等,这个表达式等同于“A+”,“A{0}”等同于“A*

{M,N}

至少M个且至多N个匹配的模式

A{1,3}可以匹配“A”,“AA”和“AAA

其他正则表达式字符

虽然在QTP的用户手册中没有列举所有的正则表达式字符,但是它支持所有的VBScript正则表达式字符。下面表13-3中列出了其他的一些正则表达式符号。

13-3 

字符

说明

举例

\d

匹配09的任意数字,等同于“[0-9].

\d{2}”可以匹配0199的所有数字,但是它不匹配19的单数

\D

匹配任意的非数字字符,等同于“[^0-9].

\D”可以匹配“A”,“&”,“$”等

\num

匹配num,其中num是一个正整数,对所获取的匹配的引用 

“(\d)\1\1”可以匹配”111”,”222”,”333”

\s

匹配任何空白字符,等同于“[\f\n\r\t\v]

\S

匹配任何非空白字符,等同于“[^\f\n\r\t\v]

问题13.1:能够匹配如下三个字符串的正则表达式是什么?“test.app.com”,”qa.app.com”,”www.app.com

在这三组字符串中可以看到都含有“.app.com”,所以我们可以用“\.app\.com”来匹配它(在“.”之前使用了符号“\”,所以这里的“.”是作为一个普通字符而不是元字符)。现在我们需要一个正则表达式,能够表示“test”“qa”“www”中的一个字符串,这个表达式是(test|qa|www)。那么这个完整的正则表达式就是“(test|qa|www)\.app\.com

问题13.2 用以匹配日期型MM/DD/YY的正则表达式如何写?

MM”可以是112的数字,其正则表达式是“[1-9]|1[0-2]”。“DD”可以是131的数字,其正则表达式是“[1-9]|[1-2][0-9]|3[0-1]”。“YY”的正则表达式则是“[0-9][0-9]”。组合起来日期型的正则表达式就是“([1-9]|1[0-2])/( [1-9]|[1-2][0-9]|3[0-1])/([0-9][0-9])”。

这其实是匹配一个含有日期的字符串,如果我们让它只匹配日期型字符串,那么可以在其前面用“^”在结束处用“$”。最终这个正则表达式就是“^([1-9]|1[0-2])/([1-9]|[1-2][0-9]|3[0-1])/[0-9][0-9]$”。

QTP中何时使用正则表达式?

如果一个预期的属性的值是动态的,但又遵循一定的模式,这种情况使用正则表达式将是最佳解决方案。例如,NameID_233NameID_256NameID_290…等等,它们遵循一个模式,都是以字符串“NameID_”开始,然后跟一个数字。这种模式可以用正则表达式“NameID_.*”来表示,这里的“.”和“*”是元字符,其意义在表13-1中已解释。符号“.”是指任何字符,跟在“.”之后的符号“*”是指0个或多个其前面的那个字符。

正则表达式也可以用来识别QTP测试对象的属性、动态值或者是有许多类似模式的情况。我们以一个网页文本框为例,所有的名称均以“Name”开头,而实际名称的模式是“Name1”、“Name2”、“Name3…等。访问这些文本框对象,我们可以用描述性编程(DP)和下面的方法。

方法一

'Create a description object for text box

Set oTextBox = Description.Create

'the regular expression to match a value starting Name

oTextBox("name").value = "name.*"

'This property by default is True,so we could skip this line of code 

oTextBox("name").RegularExpression = True

'To access the first text box whose name start with Name

oTextBox("index").value = 0

'Set the value of the text box 

Browser("").Page("").WebEdit(oTextBox).Set "Value"

方法二

‘Set the value of first text box whose name start with “Name”

Browser(“”).Page(“”).WebEdit (“name:=Name.*”,”index:=θ”).Set “Value”

In this method we can also avoid using the regular expression by generating the name at runtime as given here.

 ‘The text box to be set 

   i=1

‘Set the text box value

Browser(“”).Page(“”).WebEdit (“name:=Name” & i) .Set “Value”

如果我们的对象是放在对象库中的,那么需要在“Object Repository Manager.”中更改它的值,打开工具栏上的“Object Repository”并浏览对象,如图13-1所示。点击“Constant value options”按钮,点击“Regular Expression”复选框,在输入框中输入相应的内容,如图13-2所示。

-----------------------------------------------------------------------------------------------------------

注意:在输入文本内容后点击“Regular expression”,QTP会问我们是否要在所有的特殊字符前加“\”,如果我们想要它匹配的是按照字面意思的字符串就选择是,如果想要将它作为一种模式则选择否。

-----------------------------------------------------------------------------------------------------------

如果我们不知道字面正则表达式是什么,例如网页地址“http://www.testapp.com/Test%20Test.html”,我们可以在文本框中输入此网页地址,点击“Regular expression”复选框,然后会打开一个对话框询问是否在特殊字符前加“\”,如图13.3所示,在点击“是”之后,生成的字面正则表达式是“http://www\.testapp\.com/Test%20Test\.html

使用代码测试正则表达式

QTP通过微软VBScript引擎,提供RegExp,用于一般的正则表达式赋值和处理。下面是一个函数,用于测试匹配字符串中满足匹配要求的模式:

'Function to check if a string matches a regular expression pattern

Function IsRegEqual(s_Text, s_Pattern, MatchCompleteString)

    Dim regEx, retVal 

    

    ‘ Create regular expression object.

    Set regEx = New RegExp 

    

    ‘Set pattern.

    regEx.Pattern = s_Pattern 

    'Ignore case

    regEx.IgnoreCase = True

    'Match complete string or not

    regEx.Global = MatchCompleteString 

    

    ‘Test the regular expression

    IsRegEqual = regEx.Test(s_Text) 

    Set regEx = Nothing

End Function

Usage:

‘Will display True

Msgbox IsRegEqual(“foos”,”foo|bar”, False)

‘Will display False

Msgbox IsRegEqual(“foos”,”foo|bar”, True)

‘Will display False

Msgbox IsRegEqual(“foos”,”fo(o|b)ar”, True)

‘Will display True

Msgbox IsRegEqual(“Are you foo?”,”foo”,True)

‘Will display False . This is for a string starting and ending with foo which is foo only.

Msgbox IsRegEqual (“Are you foo?”,”^foo$”,True) 

使用正则表达式来选择WebListWebRadioGroup的值

要从WebListWebRadioGroup中选择一个值,首先我们要使用“all items”获取到对象中包含的所有项目的列表,返回的值用分号“;”隔开,然后我们使用split函数把所有的值放在数组中:

'Function for selecting a value based on regular expression

'matchIndex is a 0 based value

Function WebRegSelectValue(ByVal oObject, ByVal Pattern, ByVal matchIndex)

Dim allItemValues, matches

matches = 0

'Get all the items into an array

allItemValues = Split(ReplaceWhiteCha(oObject.GetROProperty("all items"))," ")

'Loop through all the values in the array

For i = LBound(allItemValues) to UBound(allItemValues)

'See if the current value matches the pattern

If IsRegEqual(allItemValues(i), Pattern, True) Then

'If match found is equal to the no. of matched then 

'select the value

If matches = matchIndex Then

oObject.Select i

Exit Function

End If

matches = matches + 1

End If

Next

'No matching value found. Raise error

Err.Raise vbObjectError + 1, "WebRegSelectValue","Could not find the specified value - " & Pattern & ", Index - " & matchIndex

End Function

Function IsRegEqual(s_Text, s_Pattern, MatchCompleteString)

    Dim regEx, retVal 

    

    ' Create regular expression object.

    Set regEx = New RegExp 

    

    'Set pattern.

    regEx.Pattern = s_Pattern 

    'Ignore case

    regEx.IgnoreCase = True

    'Match complete string or not

    regEx.Global = MatchCompleteString 

    

    'Test the regular expression

    IsRegEqual = regEx.Test(s_Text) 

    Set regEx = Nothing

End Function

Public Function ReplaceWhiteCha(ByVal Text)

'String pattern ending with carriage return, tab, new line

'or a space character

LTrimPattern = "[\s]"

Dim oRegExp

Set oRegExp = New RegExp

oRegExp.Pattern = LTrimPattern

oRegExp.Global = True

ReplaceWhiteCha = oRegExp.Replace(Text," ")

End Function

'Register the new function

RegisterUserFunc "WinComboBox","SelectReg","WebRegSelectValue"

Window("Flight Reservation").WinComboBox("Fly From:").SelectReg  "Frankfurt",0

使用正则表达式提取信息

正则表达式也被用来从字符串中提取信息,考虑“Your package will be shipped on 10/1/2008 and will be delivered by 15/1/2008”,现在我们用正则表达式匹配日期型模式“DD/MM/YYYY”即是“(0[1-9]|[12][0-9]|3[0-1])/(0?[1-9]|1[0-2])/([0-9]{4})

'Text from which data needs to be extracted

txtInformation = "Your package will be shipped on 10/1/2008 and will be delivered by 15/1/2008."

'Pattern to find the data

datePattern = "(0[1-9]|[12][0-9]|3[0-1])/(0?[1-9]|1[0-2])/([0-9]{4})"

Set oRegExp = New RegExp

'Set the date pattern

oRegExp.Pattern = datePattern

'Set global to true as we want all matches

oRegExp.Global = True

'Fidn the matches

Set oMatches = oRegExp.Execute(txtInformation)

If oMatches.Count <> 2 Then

MsgBox "Error could not find both dates"

Else

shippingDate = oMatches(0)

deliveryDate = oMatches(1)

MsgBox "Shipping Date - " & shippingDate

MsgBox "Delivery Date - " & deliveryDate

End If

这种方法也可以用来为从网页中提取信息。

使用正则表达式替换一个字符串的数据

正则表达式也被用来替换一个子字符串匹配给定的模式。在VBScriptLTrimRTrim函数分别用于去掉字符串的开始和结束处的空格。如果我们需要这样的一个功函数,可以去掉如新的行、制表符、回车等额外的空白字符,我们可以使用下面的代码来实现:

'Text from which data needs to be replaced

txtInformation = vbnewline & vbLf & vbTab & _

" This Is Great " _ 

& vbTab & vbLf & vbNewLine

'Function to trim white space characters from end of a string

Public Function RTrimW(ByVal Text)

'String pattern ending with carriage return, tab, new line

'or a space character

RTrimPattern = "[\s]*$"

Dim oRegExp

Set oRegExp = New RegExp

oRegExp.Pattern =RTrimPattern

oRegExp.Global = False

RTrimW = oRegExp.Replace(Text,"")

End Function

'Function to trim white space characters from start

'of a string

Public Function LTrimW(ByVal Text)

'String pattern ending with carriage return, tab, new line

'or a space character

LTrimPattern = "^[\s]*"

Dim oRegExp

Set oRegExp = New RegExp

oRegExp.Pattern = LTrimPattern

oRegExp.Global = False

LTrimW = oRegExp.Replace(Text,"")

End Function

'Function to trim white space characters from start

'and end of a string

Public Function TrimW(ByVal Text)

TrimW = LTrimW(RTrimW(Text))

End Function

MsgBox (txtInformation)

MsgBox LTrimW(txtInformation)

MsgBox RTrimW(txtInformation)

MsgBox TrimW(txtInformation)

这种方法也可以用来为从网页中提取信息。

使用正则表达式替换一个字符串的数据

正则表达式也被用来替换一个子字符串匹配给定的模式。在VBScriptLTrimRTrim函数分别用于去掉字符串的开始和结束处的空格。如果我们需要这样的一个功函数,可以去掉如新的行、制表符、回车等额外的空白字符,我们可以使用下面的代码来实现:

'Text from which data needs to be replaced

txtInformation = vbnewline & vbLf & vbTab & _

" This Is Great " _ 

& vbTab & vbLf & vbNewLine

'Function to trim white space characters from end of a string

Public Function RTrimW(ByVal Text)

'String pattern ending with carriage return, tab, new line

'or a space character

RTrimPattern = "[\s]*$"

Dim oRegExp

Set oRegExp = New RegExp

oRegExp.Pattern =RTrimPattern

oRegExp.Global = False

RTrimW = oRegExp.Replace(Text,"")

End Function

'Function to trim white space characters from start

'of a string

Public Function LTrimW(ByVal Text)

'String pattern ending with carriage return, tab, new line

'or a space character

LTrimPattern = "^[\s]*"

Dim oRegExp

Set oRegExp = New RegExp

oRegExp.Pattern = LTrimPattern

oRegExp.Global = False

LTrimW = oRegExp.Replace(Text,"")

End Function

'Function to trim white space characters from start

'and end of a string

Public Function TrimW(ByVal Text)

TrimW = LTrimW(RTrimW(Text))

End Function

MsgBox (txtInformation)

MsgBox LTrimW(txtInformation)

MsgBox RTrimW(txtInformation)

MsgBox TrimW(txtInformation)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值