Menu Bar of the blog

Friday, 26 December 2014

UFT Code#3: Understanding Regular Expressions

UFT Code#3: Understanding Regular Expressions

Following funciton is made to find:-
1. Whether a pattern is found in a string or not.
2. No. of occurences of a pattern in a given string.
3. First Index of a pattern in a given string.

str = "this that this there this was this me"
pat = "this"
findPatternMatches str, pat

Function findPatternMatches(str, pat)
   Set regex = new RegExp        'New Regular Expression Object.
   regex.Global = true            'Global Property - Sets or returns a Boolean value that indicates if a pattern should match all occurrences in an entire search string or just the first one. 
   regex.IgnoreCase = true        'IgnoreCase Property - Sets or returns a Boolean value that indicates if a pattern search is case-sensitive or not. 
   regex.Pattern = pat            'Pattern Property - Sets or returns the regular expression pattern being searched for. 
   msgbox "Pattern Found - " & regex.test(str)    'Test Method - It is used to test whether a pattern is pesent in a string or not. If it returns true, then it means pattern has been found in the string.
   Set matches = regex.Execute(str) 'Execute Method - Executes a regular expression search against a specified string. It returns a collection of objects matched during execution.
   msgbox "Total Matches Found: " & matches.Count
   For i = 0 To matches.Count-1
      Set match = matches(i)
      msgbox "Value found at index - " & match.firstindex & ". Match value is - " & match.value
   Next
End Function

Q. To find words starting with 'th' in the string 'this that there abcthxyz'?
Ans. Set pattern as '\bth' in the above function. No. of 'th' comes out to be 3.

Q. To find no. of occurrences of 12 in the string "134512678912563"?
Ans. Set pattern as "12" in the above function. No of 12 comes out to be 2.

Using the backward slash Character:-
A backward slash (\) instructs QTP to treat the next character as a literal character, if it is otherwise a special character.

Expressions & Explanation:-
1. Match any single character (.)
Example: “lear.” will match “learn”, “lear1” or “lear%”
2. Match the preceding character zero or more times (*)
Example: “zo*” matches either “z” or “zoo”.
3. Match any string starting with text specified just before the pattern (.*)
Example: “learn.*” will match any string starting with “learn”. Let us say you encounter a string “We are on letstest” but you see that the characters after the string “lets” keeps changing. In such a case you can simply make a regular expression as “We are on lets.*”
4. Match the beginning of a line (^)
Example: “^letstest” will match any line that starts with “letstest” hence it will match letstest, letstest.com but not www.letstest.com
5. Match the end of a line ($)
Example: “letstest$” will match any line that ends with “letstest” hence it will match letstest, www.letstest but not www.letstest.com
6. Match the preceding character one or more times (+)
Example: “zo+” matches “zoo” but not “z”.
7. Match the preceding character zero or one time (?)
Example: “a?ve?” matches the “ve” in “never”.

Example 1: Regular expression to match all valid email addresses
Use pattern as: ^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$

Example 2: Regular expression to match a date in MM/DD/YYYY format
Use pattern as: ^([1-9]|1[0-2])/([1-9]|[1-2][0-9]|3[0-1])/[0-9][0-9][0-9][0-9]$

Example 3: Find words ending with 'tion' in the string - 'The completion of selection process.'
Use pattern as: "\b[a-z]+tion\b"
=====================*********************************======================

No comments:

Post a Comment