Tuesday 15 December 2015

Regular Expression in UFT

Regular expression is a string to search for complex phrase.

When there is sentence which has lot of words with special characters etc in it and it is keep on changing, so it is very difficult to adopt those changes in UFT (in Object Repository/ in Script) to overcome that issue we can use Regular expressions to search for that.

Below is technical issue I come across when I was working in the project
Object property value: Testing? please use Dev 123456 environment
and Dev 123456 number changes dynamically.

my solution for this: Testing.*\W.*

As in the above property value there is ? special character which is also can be used match with regular expression and hence it doesn't recognize entire text phrase.

Here \W try to match any non-word character. Equivalent to "[^A-Za-z0-9_]".
and .* try to match any zero or more characters

Below is few details I have given for various characters which can be used in regular expression to match our phrase.

Symbol Description
Alphanumeric Matches alphabetical and numerical characters only.
\n Matches a new line.
\[ Matches [ literal only
\] Matches ] literal only
\( Matches ( literal only
\) Matches ) literal only
\t Matches horizontal tab
\v Matches vertical tab
\| Matches | literal only
\{ Matches { literal only
\} Matches } literal only
\\ Matches \ literal only
\? Matches ? literal only
\* Matches * literal only
\+ Matches + literal only
\. Matches . literal only
\b Matches any word boundary
\B Matches any non-word boundary
\f Matches a form feed
\r Matches carriage return
\xxx Matches the ASCII character of an octal number xxx.
\xdd Matches the ASCII character of an hexadecimal number dd.
\uxxxx Matches the ASCII character of an UNICODE literal xxxx.


[xyz] Match any of the character class enclosed within the character set.
[^xyz] Matches any of the character class that are NOT enclosed within the character set.
. Matches any character class except \n
\w Match any word character class. Equivalent to [a-zA-Z_0-9]
\W Match any non-word character class. Equivalent to [^a-zA-Z_0-9]
\d Match any digit class. Equivalent to [0-9].
\D Match any non-digit character class. Equivalent to [^0-9].
\s Match any space character class. Equivalent to [ \t\r\n\v\f]
\S Match any space character class. Equivalent to [^\t\r\n\v\f]


* Matches zero or more occurrences of the given regular Expression. Equivalent to {0,}.
+ Matches one or more occurrences of the given regular Expression. Equivalent to {1,}.
? Matches zero or one occurrences of the given regular Expression. Equivalent to {0,1}.
{x} Matches exactly x number of occurrences of the given regular expression.
{x,} Match atleast x or more occurrences of the given regular expression.
{x,y} Matches x to y number of occurences of the given regular expression.


0 Grouping a clause to create a clause. "(xy)?(z)" matches "xyz" or "z".
| Alternation combines one regular expression clause and then matches any of the individual clauses. "(ij)|(23)|(pq)" matches "ij" or "23" or "pq".


"^\s*.." and "..\s*$" Represents that there can be any number of leading and trailing space characters in a single line.
"((\$\s?)|(#\s?))?" Represents an optional $ or # sign followed by an optional space.
"((\d+(\.(\d\d)?)?))" Represents that at least one digit is present followed by an optional decimals and two digits after decimals.

No comments:

Post a Comment