Introduction to Regular Expressions in VBScript
This post talks about regular expressions in VBScript, regular expressions in UFT, regular expressions in VBScript examples, regular expressions in UFT examples, using regular expressions in VBScript, using regular expressions in UFT, VBScript RegExp Object, UFT RegExp Object, regular Expression, How to use regular expression.
VBScript does not support regular expression constant (/a pattern/ ). VBScript provides a pattern properly of a RagExp object.
Regular Expressions in QTP/UFT/VBScript:
What is Regular Expression?
It is a way of representing data using symbols. They are often used within matching, searching or replacing algorithms. Regular expressions enable us to perform the following tasks.
- Pattern matching and replace.
- Searching and replace.
- Replace
- Identifying required number or string
Text strings get assigned to the pattern property of a RegEx object.
Regular Expressions in QTP/UFT:
Regular expressions can be used in QTP/UFT for identifying objects and text strings with varying values.
Where we use regular expression:
o Defining the property values of an object in Descriptive programming for handling dynamic objects
o For parameterizing a step
o creating checkpoints with varying values
Using Regular Expressions in QTP or UFT:
We can define a regular expression for a constant value, a Data Table parameter value, an Environment parameter value, or a property value in Descriptive programming.
We can define a regular expression in a standard checkpoint to verify the property values of an object; we can set the expected value of an object’s property as a regular expression so that an object with a varying value can be verified.
We can define the text string as a regular expression when creating a text checkpoint to check that a varying text string is displayed on our application,
For XML checkpoints we can set attribute or element values as regular expressions.
RegExp object
VB Script is providing RegExp object for defining Regular expressions, It provides simple support for defining regular expressions. RegExp comes with predefined properties and methods.
Regular Expression Object Properties and Methods:
Properties:
a) Global Property
b) IgnoreCase Property
c) Pattern Property
Methods:
a) Execute() Method
b) Replace() Method
c) Test() Method
Global Property
The global property accepts two values.
- True– it signifies the search will happen to the entire string.
- False– searches for the first occurrence
Syntax
Object.Global= value
By default global property is set to false. The Boolean value determines whether all occurrences of a pattern should be replaced.
Example with /b switch and without /b switch
IgnoreCase Property
This is helpful when we want to search a text.(In case – insensitive manner). Ignore case property accept two values.
- True– it will not ignore the case while searching
- False– it will make the search case sensitive
syntax-
Object.IgnoreCase= value
The Boolean value determines whether a case-sensitive search needs to be performed.
Dim reg, strString Set reg=New RegExp reg.Pattern="/b of" //(/b matches the exact word) reg.Global=True reg.IgnoreCase=True strString="To take OF is not easy of the ground." msgbox reg.Replace(strString,"off")
O/P- To take off is not easy on the ground.
Pattern Property
The pattern property takes the flags and text pattern that needs to be searched. This property sets or returns the regular expression that needs to be searched.
syntax-
Object.Pattern=[(flag info)("Search String/search info")]
The argument can be a string or a regular expression itself. Flag info is optional here.
Regular Expressions Examples or How to use Regular Expression?
- We need to create a new regular expression object
set myVar=New RegExp
- We need to provide the key that needs to be matched.
myVar.pattern="[[mySearchText]]"
- myLine- “the line or string from where the matching will happen”
- Ask VBscript engine to perform the pattern matching and perform your task.
msgbox myVar.Replace( myLine,[[text after replacement]])
Example of Regular Expression
Dim myVar,myLine Set myVar=New RegExp //regular expression object myVar.Pattern="boy" //creates pattern myLine="every boy is great" //initial line msgbox myVar.Replace( myLine,"girl")
o/p- every girl is great
How to make it dynamic?
We can make it dynamic by introducing an input box.regular expressions in VBScript examples
Dim myVar, myLine, myWord Set myVar=New RegExp myLine= inputbox(" Enter your line") myVar.pattern= inputbox(" enter a word to change") myWord= inputbox(" enter a word that will be final") msgbox myVar.replace( myLine, myWord)
For more dynamic behavior we can create a function, it will help us achieve CD-CT-CI.
It will also help in unattended execution.
regular expressions in VBScript examples/VBScript RegExp Object
Function ChangeLine(sLine, sWordToReplace, sNewWord) Dim myVar,myLine,myWord Set myVar==New RegExp myLine=sline myVar.Pattern= sWordToReplace myWord=sNewWord ChangeLine=myVar.Replace( myLine, myWord) End function
The above example will only change the first occurrence of the word in the line. But if the line has many words( that needs to be changed) it will not work fully. We need to change the script a little bit.
myVar.Pattern= "switch and the search word" myVar.Global= True
Switch mainly used is /b (word boundary)
The Global property ensures match in all occurrence of the word.
1) Match File Names in a Directory against Regular Expression
regular expressions in VBScript examples/VBScript RegExp Object
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
strCurrentDirectory = objShell.CurrentDirectory
Set objFolder = objFS.GetFolder(strCurrentDirectory)
Set colFiles = objFolder.Files
Set objRE = New RegExp
objRE.Global = True
objRE.IgnoreCase = False
objRE.Pattern = WScript.Arguments(0)
For Each objFile In colFiles
bMatch = objRE.Test(objFile.Name)
If bMatch Then
WScript.Echo objFile.Name
End If
Next
2) Match Content in a File against a Regular Expression
regular expressions in VBScript examples/VBScript RegExp Object
strFileName = "E:test.txt"
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objTS = objFS.OpenTextFile(strFileName)
strFileContents = objTS.ReadAll
WScript.Echo "Searching Within: "
WScript.Echo strFileContents
objTS.Close
Set objRE = New RegExp
objRE.Global = True
objRE.IgnoreCase = False
objRE.Pattern = WScript.Arguments(0)
Set colMatches = objRE.Execute(strFileContents)
WScript.Echo vbNewLine & "Resulting Matches:"
For Each objMatch In colMatches
WScript.Echo "At position " & objMatch.FirstIndex & " matched " & objMatch.Value
3. Replace Function
regular expressions in VBScript examples/VBScript RegExp Object
Dim myPattern,myString Set myPattern=new RegExp myPattern.pattern="[2,3,4,5,6,7] myString="I have 2 glasses with 3 tablespoon" msgbox myPattern.Replace (myString,"many")
4. How to match digit characters with a regular expressions?
regular expressions in VBScript examples/VBScript RegExp Object
Dim myReg,myString Set myReg=New RegExp myReg.Pattern="[0123456789]" //this is for specific myReg.Pattern="[0-9]" //this is for generic myReg.Pattern="/d" //this is for generic myString="There are 5 persons in the car" msgbox myReg.Replace(myString,"Many")
O/P- There are many persons in the car.
5.How not to match a digit in a regular expression?
A caret (^) or a circumflex sign does the trick here.
“[^,0-9]”=> will not match any digit in the range 0-9
“[^\d]”=> does the same
“[\D]”=> does the same
How to create a pattern or range in a regular expression?
While the above example like”[ 0 1 2 3 4 5 6 7 8 9]”or “[0-9]” or “/d” provides a generic pattern.We can create our own pattern by providing range or repeat count.
- “\d” implies \d{1} that is single digit
- “\d{2}” implies double digit
- “\d{3}” implies three digit
How to specify minimum number or range using regular expression?
- “\d+”=>implies that one or more digits equivalent to\d{1,}
- “\d*”=>implies that zero or more digits equivalent to \d{0,}
- “\d?”=>implies that zero or one equivalent to \d{0,1}
we need to be careful using the last two.In order to change all the digits in the given string we need to Set global property as true.
Remembered matches(regular expressions in vbscript)
This technique is useful when one or all of the text matches, with our pattern while finding or replacing text.
regular expressions in vbscript examples/VBScript RegExp Object
Dim myReg,myString,myCol,matchFound Set myReg=New RegExp myReg.Global=True myReg.Pattern="http://(\w+[\w-]*\w+\.)*\w+" myString="http://www.google.com" myString=myString&VBcrlf & "http://www.bing.com" myString=myString&VBcrlf & "http://www.altavista.com" Set myCol=myReg.Execute(myString) For Each matchFound in myCol msgbox "Found valid URL" & matchFound.value Next
The URL pattern starts with “http://”always. Hence we need to hardcode it. Now the remaining pattern starts with \w that matches with [a-z A-Z 0-9]i.e then alphanumeric characters .
The second component is [\W-] that matches with any alphanumeric characters with a dash so if the URL is having a dash in between ,the code will be able to determine .
*is used for repetition.
Execute Method
Execute method searches a specific string and return matches as collection.
Syntax-
object.Execute(stringPattern)
Example
Dim myReg,myString,myCol,matchFound Set myReg=New RegExp myReg.Global=True myReg.Pattern="http://(\w+[\w-]*\w+\.)*\w+" myString="http://www.google.com" myString=myString&VBcrlf & "http://www.bing.com" myString=myString&VBcrlf & "http://www.altavista.com" Set myCol=myReg.Execute(myString) For Each matchFound in myCol msgbox "Found valid URL" & matchFound.value Next
If the Execute method does not have matched content, in that case also ,execute return a collection.It will be an empty collection.
Replace Method
Replace method searches and replaces a string specified with the search string .
Syntax-
object replace (String to search,string to replace)
Back referencing
Back referencing is a technique that allows us to stare some part of the string in the buffer(temporary). It is done with the help of a parentheses.We can override the saved part of the string with other regular expressions(“?:”,”?=”,”?!”)
Example
Dim MyReg,Mystring Set MyReg=New RegExp MyReg.Pattern="(\s+)\s+(\s+)\s+(\s+)\s+(\s+)\s+(\s+)" MyString=This is not so cool” Msgbox MyReg.Replace(s,$1,$2,$4,$5)
Op->This is so cool
Test Method
Test method searches a string by executing a regular expression.It returns Boolean –true to indicate the pattern is found and false –to indicate that the pattern is not found.
Syntax –
object.test (searchString)
Example
Dim MyRag,MyString Set MyReg=New RegExp MyReg.IgnoreCase=TRUE MyReg.Pattern="http://(\W+[\W-]*\W+\-)*\W+" MyString="My favourite search engine http://www.google.com" If MyReg.Test(MyString)then Msgbox "the string is having an URL" Else Msgbox "The string does not have a valid URL" End If
O/P ->The string is having an URL
The Matches Collection
This matches collection is a placeholders for match objects.Execute method returns this collection.If execute method is executed we get zero or more match objects in the collection.The below properties can be extracted from match object
- The string
- The length of the string
- The index value where the match is found
Note
We need to enable Global property to true to match in the whole string.Match support two properties
- Count-It returns the number of items present in the collection.
- Item-It returns the exact item based or the query on the list with a key as Index .
Match object periods three read only properties-
- First Index
- Length
- Value
The details are as follows
- First Index-returns the position where the match happens with a giving string.
Syntax-Object.FirstIndex
- Length-returns the length of a match in the search string.
Syntax-Object.Length
- Value- returns the value or text or string of a match found in a search string.
Syntax-Object.value
How to validate a phone number?(XXX)XXXX-(XXX)
Dim myReg,MyNumber Set myReg=New RegExp MyReg.pattern='\([0-9]{3}\[0-9]{4}-(0-9){3} MyReg.Global=True MyReg.Ignorecase=True MyNumber=Inputbox("Enter your mobile in (xxx)xxxx-xxx format") If MyReg.test(MyNumber)then msgbox "It is a valid mobile number" Else msgbox "It is not a valid mobile number" End if
How to find the white space?
The Syntax –”^[\t]*$”
- ^ matches the start of the line.
- [\t] matches tab character for finding space or more space characters.
- $ Matches end of the line.
Dim MyReg,MyString,objMatch,spaceMatch,sMsg Set MyReg=New RegExp MyReg.Global=True MyReg.pattern="^[\t]*$" Mystring="I have a big cat" Set spaceMatch=MyReg.Execute(Mystring) SMsg="" For Each objMatch in spaceMatch sMsg="Space found at position" objMatch.FirstIndexNumber msgbox sMsg Next
6.String Manipulation using Regular Expression
Making Global property True
specific match or using \b switch
Dim reg, strString Set reg=New RegExp reg.Pattern="/b of" //(/b matches the exact word) reg.Global=True strString="To take of is not easy of the ground. It needs a great offline practice." msgbox reg.Replace(strString,"off")
As the Global property is true , it matches with all words in the string.
Making Global property False
Dim reg, strString Set reg=New RegExp reg.Pattern="/b of" //(/b matches the exact word) reg.Global=False strString="To take of is not easy of the ground. It needs a great off line practice." msgbox reg.Replace(strString,"off")
O/P—>to take off is not easy of the ground.It needs a great off line practice. Only changes the first occurrence.
Regular Expression cheat seat:
The below patterns can be used with VBScript RegExp Object.
Character | Description |
---|---|
Indicates that the next character is either special character or literal | |
^ | Indicates a match at the beginning of the input |
$ | Indicates a match at the end of the input |
* | Indicates a match before the indicated character.Matches will be performed either zero or more times. |
+ | Indicates a match before the indicated character.Matches will be performed either one or more times. |
? | Indicates a match before the indicated character.Matches will be performed either zero or one time.It can also matches any single character except a newline character. |
(pattern) | Indicates a match with the given pattern.It returns a collection of items.To retrieve the pattern info we can use item[0],item[1],….item[n] |
(?:pattern) | This is a non Capturing search hence the match is not captures for future use.It is useful for putting OR condition |
(?=pattern) | This is a non Capturing search hence the match is not captures for future use.However it is a positive look head that matches the search string at any given point of time. Test(?1,2,3,8,9) matches Test in TestCase1,TestCase2…TestCase9 but not in TestData |
(?!pattern) | This is a non Capturing search hence the match is not captures for future use.However it is a negative look head that matches the search string at any given point of time. Test(?!1,2,3,8,9) matches Test in TestData but not in TestCase1,TestCase2…TestCase9 |
x|y | Indicates a match with a match will happen with either x or y |
{n} | Indicates a match will happen exactly n times,where n is greater than zero. |
{n,} | Indicates a match will happen at least n times where n is greater than zero and it must terminate with a comma. |
{n,m} | Indicates a match will happen at least n times and at most m times where n and m are greater than zero and m is also greater than n. |
[xyz] | Indicates a match with any character from the given character set. |
[^xyz] | Indicates a match with any other character which are not part of the given character set. |
[a-z] [A-Z] | Indicates a match with any character in the given range. |
[^m-z] | Indicates a match with a negative range of character. |
b | Indicates a match with a boundary for word.It identifies the positioning between a word and a space. |
S | Indicates a match with all non white space just opposite to /s |
B | Indicates a match with a non word Boundary. |
d | Indicates a match with a digit character. |
D | Indicates a match with a non digit character. |
f | Indicates a match with a form feed character. |
n | Indicates a match with a new line character |
r | Indicates a match with a carriage return character |
s | Indicates a match with any white space character including space,tab etc. It is equivalent to [fnrtv] |
w | Indicates a match with the word character including underscore [A-Za-z0-9_] |
t | Indicates a match with a tab character |
W | Indicates a match with the non word character like [^A-Za-z0-9_] |
. | Indicates a match with the dot |
| | Indicates a match with a Pipe |
{ | Indicates a match with the curly bracket start”{“ |
} | Indicates a match with the curly bracket end “}” |
Indicates a match with backslash () | |
[ | Indicates a match with the third bracket start “[“ |
] | Indicates a match with the third bracket end “]” |
( | Indicates a match with the first bracket start “(“ |
) | Indicates a match with the first bracket end “)” |
$num | Indicates a match with a positive number. |
v | Indicates a match with a vertical tab character |
n | Indicates a match with an Octal value may be 1,2,3 digits |
uxxxx | Indicates a match with an ASCII character represented by Unicode-XXXX |
xn | Indicates a match with n,where n is a hexadecimal value of 2 digits |
[m-z] | Indicates a match with a character not in the given range.Ways of Regular Expressions:regular expressions in vbscriptBackslash Character:A backslash () can serve two purposes. It can be used in conjunction with a special character to indicate that the next character be treated as a literal character. Matching Any Single Character:A period (.) instructs QTP to search for any single character (except for n). Matching Any Single Character in a List:Square brackets instruct QTP to search for any single character within a list of characters. 186[789] Matching Any Single Character Not in a List:When a caret (^) is the first character inside square brackets, it instructs QTP to match any character in the list except for the ones specified in the string. Matching Any Single Character within a Range:To match a single character within a range, we can use square brackets ([ ]) with the hyphen (-) character. 201[0-9] Matching Zero or More Specific Characters:An asterisk (*) instructs QTP to match zero or more occurrences of the preceding character. Matching One or More Specific Characters:A plus sign (+) instructs QTP to match one or more occurrences of the preceding character. Matching Zero or One Specific Character:A question mark (?) instructs QTP to match zero or one occurrences of the preceding character. Grouping Regular Expressions:Parentheses (()) instruct QTP to treat the contained sequence as a unit, just as in mathematics and programming languages. Using groups is especially useful for delimiting the argument(s) to an alternation operator ( | ) or a repetition operator ( * , + , ? , { } ). Matching One of Several Regular Expressions:A vertical line (|) instructs QTP to match one of a choice of expressions. Matching the Beginning of a Line:A caret (^) instructs QTP to match the expression only at the start of a line, or after a newline character. Matching the End of a Line:A dollar sign ($) instructs QTP to match the expression only at the end of a line, or before a newline character. Further visit: Learn Quickly About Class VirtualMachineError in Java in 5 Minutes Matching Any AlphaNumeric Character Including the Underscore:w instructs QTP to match any alphanumeric character and the underscore (A-Z, a-z, 0-9, _). Matching Any Non-AlphaNumeric Character:W instructs QTP to match any character other than alphanumeric characters and underscores. Combining Regular Expression Operators:We can combine regular expression operators in a single expression to achieve the exact search criteria we need. |
Related Articles
The post Regular Expressions in VBScript or QTP or UFT: Complete Guide in 5 Minutes appeared first on Tech Travel Hub.