Using Regular Expressions in Scripts

Applies to TestComplete 15.70, last modified on December 17, 2024

A regular expression is a string that describes a search pattern. It defines one or more substrings to find in a text fragment.

Regular expressions consist of:

  • Literal symbols.

  • Tokens that denote non-printable characters, digits, and alphanumeric characters

  • Regular expression engine’s instructions

TestComplete supports native regular expressions (the ones that are built into scripting language engines) and non-native regular expressions (its own regular expressions). Which syntax to use depends on the area of the application.

This topic describes native regular expressions and their syntax. You use them to work with strings in script tests (search for string values, replace them, split them, and so on).

Native regular expressions in scripting languages

How regular expressions are implemented depends on the scripting language you use:

JavaScript, JScript, C#Script and C++Script Regular Expressions

In JavaScript, JScript, C#Script and C++Script, you can create regular expressions as literals or by using the new RegExp constructor:

JavaScript, JScript, C#Script, C++Script

re = /pattern/[flags];
re2 = new RegExp("pattern"[, "flags"]);

In both lines above, pattern defines which pattern this regular expression will use, and flags defines how to apply the pattern. The available flags are: "g" - perform a global search (that is, search for all occurrences of pattern, rather than just the first one), "i" - perform a search ignoring the letter case, and "m" - perform a multi-line search. Flags can be combined.

Below is an example of how to create a regular expression object:

JavaScript, JScript, C#Script, C++Script

re = /gr[ae]y/im;
re2 = new RegExp("gr[ae]y", "im");

Each line above creates an instance of the Regular Expression object that describes a particular regular expression.

Each instance of the Regular Expression object has the following properties and methods:

Name Description
RegularExpressionObj.compile(pattern[, flags]) Method. Compiles a regular expression into an internal format for faster execution.
RegularExpressionObj.exec(str) Method. Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.
RegularExpressionObj.test(str) Method. Returns a Boolean value that indicates whether a pattern exists in the searched string.
RegularExpressionObj.global Property. Returns a Boolean value indicating the state of the global search flag used with a regular expression.
RegularExpressionObj.ignoreCase Property. Returns a Boolean value indicating if a pattern search is case-sensitive or not.
RegularExpressionObj.multiline Property. Returns a Boolean value indicating the state of the multiline flag used with a regular expression.
RegularExpressionObj.source Property. Returns a copy of the text of the regular expression pattern.

To get the results of a regular expression search, use another object - the global RegExp object. The properties of this object update their values every time a new match is found.

Name Description
RegExp.$1...RegExpr.$9 Properties. Returns the nine most-recently memorized portions found during pattern matching.
RegExp.index Property. Returns the character position where the first successful match begins in a searched string.
RegExp.input Property. Returns the string against which a regular expression search was performed.
RegExp.lastIndex Property. Returns the character position where the next match begins in a searched string.
RegExp.lastMatch Property. Returns the last matched characters from any regular expression search.
RegExp.lastParen Property. Returns the last parenthesized submatch from any regular expression search, if any.
RegExp.leftContext Property. Returns the characters from the beginning of a searched string up to the position before the beginning of the last match.
RegExp.rightContext Property. Returns the characters from the position following the last match to the end of the searched string.

String objects in JavaScript, JScript, C#Script and C++Script also have several methods that use regular expressions:

Name Description
strObj.match(rgExp) Method. Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.
strObj.replace(rgExp, replaceText) Method. Returns a copy of a string with text replaced using a regular expression or search string.
strObj.search(rgExp) Method. Returns the position of the first substring match in a regular expression search.
strObj.split([separator[, limit]]) Method. Returns the array of strings that results when a string is separated into substrings. The separator parameter can contain either a separator symbol or a regular expression that defines it.

For a detailed description of how to use regular expressions, see the Regular Expression article on the Mozilla Developer Network if you use JavaScript, and the Introduction to Regular Expressions article in the MSDN library if you use JScript.

Python Regular Expressions

In Python, support for regular expressions is implemented via the re module. This module is not added to your project by default. To import it, add the import re line at the beginning of your test:

Python

import re

The methods used to match regular expressions in Python return a match object on success. This object always has a boolean value of True.

Properties are specified in the method call. For example, to make your regular expression ignore case:

Python

re.compile(r"^(.+)\n((?:\n.+)+)", re.MULTILINE)

The table below lists the methods and properties that can be used to work with regular expressions in Python:

Property, Method Description
re.compile(pattern, flags=0) Method. Compiles a regular expression pattern into a regular expression object, which can be used for matching using the match() and search() methods.
re.A or re.ASCII Property. When specified, \w, \W, \b, \B, \d, \D, \s and \S perform ASCII-only matching instead of full Unicode matching.
re.DEBUG Method. Returns debug information about the compiled expression.
re.I or re.IGNORECASE Property. When specified, case-insensitive matching is performed. For example, [A-Z] will match lowercase letters.
re.L or re.LOCALE Property. When specified, \w, \W, \b, \B, \s and \S depend on the current locale. The use of this flag is discouraged as the locale mechanism is very unreliable.
re.M or re.MULTILINE Property. When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline); and the pattern character '$' matches at the end of the string and at the end of each line (immediately preceding each newline).
re.S or re.DOTALL Property. When specified, the '.' special character matches all characters, including the newline character.
re.X or re.VERBOSE Property. When specified, whitespace characters and all the characters on a line after the unescaped # character are ignored.
re.search(pattern, string, flags=0) Method. Searches for the first match in a string and returns the corresponding match object. Returns None if no match is found.
re.match(pattern, string, flags=0) Method. Searches the beginning of a string for a match to the regular expression pattern and returns the corresponding match object. Returns None if no match is found.
re.fullmatch(pattern, string, flags=0) Method. If the whole string matches the regular expression pattern, returns the corresponding match object. Returns None otherwise.
re.split(pattern, string, maxsplit=0, flags=0) Method. Splits the string by occurrences of the pattern and returns it as a list. If capturing parentheses are used, the text of all groups is also returned as part of the resulting list. If maxsplit is not zero, after the specified number of splits, the remainder of the string is returned as the final element of the list.
re.findall(pattern, string, flags=0) Method. Returns all non-overlapping matches of the pattern in the string as a list of strings. The string is scanned left-to-right, and matches are returned in the order found.
re.finditer(pattern, string, flags=0) Method. Returns an iterator for match objects across non-overlapping matches for the pattern in the string. The string is scanned left-to-right, and matches are returned in the order found.
re.sub(pattern, repl, string, count=0, flags=0) Method. Returns the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in the string with repl, which can be a string or a function. If it is a string, any backslash escapes in it are processed. If repl is a function, it is called for every non-overlapping occurrence of the pattern.
re.subn(pattern, repl, string, count=0, flags=0) Method. The same as re.sub, but returns a tuple(new_string, number_of_subs_made)
re.escape(string) Method. Escapes all characters in a string.
re.purge() Method. Clears the regular expression's cache.
exception re.error Exception raised when a string is not a valid regular expression or when an error occurs during compilation or matching.

For detailed information on how to use regular expressions in Python, refer to the re — Regular expression operations article in Python Wiki.

VBScript Regular Expressions

In VBScript, use the RegExp object to create regular expressions:

VBScript

Sub Test
  Dim re, matches, match

  Set re = New RegExp
  re.Pattern = "gr[ae]y"
  re.IgnoreCase = True
  re.Global = True ' Find all occurrences of the pattern

  Set matches = re.Execute("Do you spell it gray or grey?")
  For Each match In Matches
    Log.Message(match.Value)
  Next
End Sub

The RegExp object stores a regular expression pattern along with flags that identify how to apply it. The RegExp object has the following properties and methods:

Name Description
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.
IgnoreCase Property. Sets or returns a Boolean value that indicates if a pattern search is case-sensitive or not.
Pattern Property. Sets or returns the regular expression pattern being searched for.
Execute(string) Method. Executes a regular expression search against a specified string and returns the Matches collection.
Replace(string1, string2) Method. Replaces text found in a regular expression search.
Test(string) Method. Executes a regular expression search against a specified string and returns a Boolean value that indicates if a pattern match was found.

To iterate through the matches found by the RegExp.Execute method, use the Matches and Match objects. The Matches object holds a collection of Match objects (each object describes a particular match). The Match object has the following properties and methods:

Name Description
FirstIndex Property. Returns the position in a search string where a match occurs.
Length Property. Returns the length of a match found in a search string.
Value Property. Returns the value or text of a match found in a search string.
SubMatches(index) Property. Returns the value or text of a regular expression submatch that is identified by its index.

For more information about VBScript regular expressions, see the following MSDN articles:

DelphiScript Regular Expressions

For DelphiScript, regular expressions support is implemented with the RegExpr object provided by the HISUtils plugin. The object instances define the regular expression pattern and store other properties that identify how to apply the pattern. It also has methods that are required to perform actions over regular expressions. The table below lists the methods and properties of RegExpr object:

Property, Method Description
RegExprObj.CompilerErrorPos Property. Returns the position in the regular expression where the compilation error occurred.
RegExprObj.ErrorMsg Property. Returns the description for the specified error.
RegExprObj.Expression Property. Specifies the regular expression pattern.
RegExprObj.InputString Property. Specifies the input text to which the regular expression is applied.
RegExprObj.LastError Property. Returns the code of the last occurred compilation error.
RegExprObj.LinePairedSeparator Property. Specifies characters that are treated as double-character line breaks.
RegExprObj.LineSeparators Property. Specifies characters that are treated as single-character line break.
RegExprObj.Match Property. Returns the text that matches an expression or sub-expression.
RegExprObj.MatchLen Property. Returns the length of the matching text.
RegExprObj.MatchPos Property. Returns the first character position where the matching text begins.
RegExprObj.ModifierStr Property. Specifies the string for toggling regular expression modifier flags.
RegExprObj.SpaceChars Property. Specifies characters that are treated as "whitespace characters".
RegExprObj.SubExprMatchCount Property. Returns the number of sub-expressions found in the regular expression.
RegExprObj.WordChars Property. Specifies characters that are treated as "word characters".
RegExprObj.Compile Method. Converts the literal regular expression into the engine’s internal representation.
RegExprObj.Exec Method. Searches the specified input string for the text matching the regular expression.
RegExprObj.ExecNext Method. Searches the input string for the next occurrence of text matching the regular expression.
RegExprObj.ExecPos Method. Searches for text matching the regular expression from the specified position in the input string.
RegExprObj.Replace Method. Replaces the found pattern match with the specified text.
RegExprObj.Substitute Method. Substitutes special marks in the template with found pattern matches.

Syntax reference

Tokens

The following table lists tokens that are recognized by JavaScript’s, JScript’s, Python’s and VBScript’s “native” regular expressions, and the HISUtils.RegExpr object in DelphiScript.

Token Description
^ Beginning of a line. For instance, the ^a search pattern lets you find all lines that start with a.
$ End of a line. For instance, the a$ search pattern lets you find all lines that end with a.
. Matches any single character, except for a newline one. To search for any symbol including a newline one, you can use the [\s\S] pattern or enable the single-line mode with the (?s) modifier.
* The asterisk is a symbol-“repeater”. * means 0 or more occurrences of the preceding character or sub-expression. For instance, the abc*d pattern matches abd, abcd, abccd, but not a or abcx. The .* pattern matches a string of any length (including the empty string) that does not contain the newline symbol. The * token is equivalent to {0,}.
+ The plus is a symbol-“repeater”. + indicates 1 or more occurrences of the preceding character or sub-expression. For instance, the ab+d pattern matches abbd or abbbd, but does not match abcd or abbcd. The + token is equivalent to {1,}.
? The question mark means 0 or one occurrence of the preceding character or sub-expression. For example, abc?d will find abd and abcd, but not abccd. The ? token is an equivalent to {0,1}.
a{n} n occurrences of a. For example, fo{2}t will find foot, but not fot or fooot.
a{n,} n or more occurrences of a. For example, ab{2,}c will find abbc, abbbc, abbbbc, but not abc.
a{n,m} n or more, but less than or equal to m occurrences of a. For instance, ab{2,3}c will find abbc, but not abc or abbbbc.

Note:

The ? token can be used after *, +, {n,} and {n,m}. At that, it makes the searching pattern non-greedy, that is, the pattern matches as few characters or sub-expressions as possible. For example, when searching the abbbbbcd string, the b{3,} pattern will return bbbbb, while b{3,}? will return bbb (that is, without the question mark you get the string of five symbols “b”, while with question mark you get a string of three symbols “b”). Similarly, when searching the same string using the b{2,4} pattern, you will get bbbb; while using b{2,4}? you’ll get bb.
[ ] Any single character specified in brackets. For instance, d[ab]e matches dae or dbe and does not match dwe or dxe. To include the ] character into the search, make it either first, or last character in the range or use \]. For example, []abc], [abc]] or [ab\]cd].
[^ ] Any single character except those that are specified in brackets. For instance, d[^bc]e matches dae or dxe, but not dbe or dce. d[^b-d]e matches dae, but not dbe, dce or dde.
[a-b] Any single character from a to b, inclusive. For instance d[k-x]e matches dke, dme and dxe, but not dze. To include the - character into the search, make it either first, or last character in the range or use \-. For example, [-ab], [abc-] or [a\-z].
[^a-b] Any single character not in the range a through b. For instance a[^k-x]z matches abz, aiz and ayz, but not akz.
(aaa) Denotes a sub-expression. For instance, the (abra)(kadabra) pattern contains two sub-expressions: abra and kadabra. To specify a round bracket that should be treated literally, follow it with backslash: \( or \).
a|b Either a or b. For instance, ab|cde matches ab and cde, but not abde. The ht(m|ml) pattern will find htm and html, but not htl.
\ Backslash is used to specify that special characters, such as ^, $ or . (dot), do not belong to the search pattern and should be treated literally. For instance, the \$10 pattern lets you find the $10 string. To search for a backslash, use the double backslash pattern (\\).
Some more examples:

d[a\-c]e will find dae, d-e or dce, but not dbe.

d[\^bc]e will find d^e, dbe or dce.

\xNN A symbol whose hexadecimal ASCII code is NN. For example, A\x31B will find the string A1B. (Hexadecimal 31 is ASCII code of 1).

You can also use \x{NNNN} to search for characters whose code occupies more than one byte (Unicode).

\t Tab character.
\n Newline character.
\r Carriage return character.
\f Form feed character.
\w “Word” character: an alphanumeric symbol or underscore (_). This token is equivalent to [A-Za-z0-9_].
\W Any symbol except for “word” characters. This token is equivalent to [^A-Za-z0-9_].
\d Any digit character. This token is equivalent to [0-9].
\D Any character except for digit. This token is equivalent to [^0-9].
\s “Whitespace” character: a space, tab (\t), newline (\n), carriage return (\r) and form feed (\f). This token is equivalent to [ \t\n\r\f].
\S Any symbol except for “whitespace” characters. This token is equivalent to [^ \t\n\r\f].
\b Indicates a word boundary, that is, a position between a word character and a whitespace character. For example, oo\b matches oo in foo, but not in foot. Similarly, \bfo matches fo in foot, but not in afoot.
\B Indicates any position in a word except for boundary. For example, oo\B matches oo in foot, but not in foo.

Note:

You can use the \t, \w, \W, \d, \D, \s, \S, \b and \B expressions within brackets. For example, b[\d-e]b will find b1b, b2b or beb.

Sub-expressions

You can divide an expression into constituent parts or sub-expressions. To specify a sub-expression use parenthesis, for instance, (\s\d+,\d+,d+,)(\d+). The parsing engine detects two sub-expressions in this expression:

  • \s\d+,\d+,d+,
  • \d+

Besides, the engine assigns an item index to the whole expression and to each sub-expression, where the index of the expression is 0, the index of the first sub-expression is 1 and so on. That is:

  • 0: (\s\d+,\d+,d+,)(\d+)
  • 1: \s\d+,\d+,d+,
  • 2: \d+

A text fragment that matches a sub-expression is called a submatch. How to access a submatch depends on the scripting language:

Scripting language Description
JavaScript,
JScript,
C#Script,
C++Script
The submatches are accessed via the $1$9 properties of global object RegExp. These properties return text fragments that correspond to first, second, … ninth sub-expression of the last found match. For example, RegExp.$2 returns the second submatch.
Python You can access submatches by using the group() method of the match object. If the index parameter of the method is 0, then it returns the entire match. If the index is a positive integer n, then the method returns the n-th submatch. For example, m.group(3) returns the third submatch.
VBScript Each found match is represented as the Match object, that has the SubMatches collection that stores data regarding found submatches. For example, Matches(2).SubMatches(0) refers to first submatch of the third found matching fragment.
DelphiScript To refer to a submatch you can use the RegExpr.Match property. If the Index parameter of this property is 0, then it returns fragment that matches the whole expression. If the Index is a positive integer number n, then the property returns a fragment matching the n-th sub-expression. For example, Match[1] returns the first submatch. Besides, you can use the RegExpr.Substitute method that creates a new string by replacing special characters with found matches and submatches.

Mode modifiers

VBScript does not support mode modifiers.

Mode modifiers specify how the engine interprets regular expressions. They toggle the engine’s behavior modes. The following modifiers are available:

Modifier Key Default State Description
i Enabled Makes the pattern match case-insensitive.
m Disabled Treats a string as multiple lines. In this mode, the caret ^ and dollar $ match before and after newlines in the subject string.
s Enabled Treats a string as a single line. In this mode, the dot matches the newline symbol.
g Enabled Controls greedy mode. Non-standard modifier.

Greedy repetition operator takes as many matching characters as possible, non-greedy takes as few as possible. For example, b+ and b* applied to string abbbbc will return bbbb, whereas b+? will return b and b*? will return an empty string.

Switching to non-greedy mode makes + work as +?, * as *? and so on.

x Disabled Permits whitespaces and comments in the pattern. Non-standard modifier.

In this mode, the whitespaces (\s) that are not backslashed nor within a character class are ignored. You can use this to break up your regular expression into more readable parts. Also the # character is treated as a metacharacter introducing a comment. For example:

  ( # This pattern matches
    (this) # the occurrence of 'this'
      | # or
    (that) # the occurrence of 'that'
  )

If you want to place a whitespace or # characters in the pattern, then you have to prefix them with / or encode them using hex notations (\xNN).

You can specify the modifier in the expression using the (?key) or (?-key) syntax (where key stands for the modifier key and minus sign specifies the disabled state for the corresponding modifier). If you try to specify unsupported modifier keys an error occurs. The modifiers can be applied to the whole expression or only to a sub-expression. For example:

(?i)Las Vegas matches Las vegas and Las Vegas
(?i)Las (?-i)Vegas matches Las Vegas but not Las vegas
(?i)(Las )?Vegas matches Las vegas and las vegas
((?i)Las )?Vegas matches las Vegas, but not las vegas

Examples

The code snippets below shows how to solve some practical tasks using regular expressions.

Search for text fragments

One of the most typical applications of regular expressions is searching for some text fragments within a text. In some cases, you can perform this task without using regular expressions, for instance, make a list of sought for fragments and compare the whole text with the list, but this approach is not effective when the number of possible sought for fragments is large. The regular expressions are designed especially for these situations. Using them you can define a set of rules that the fragment should comply with.

An e-mail address search is a perfect example to illustrate the abilities of regular expressions. Suppose you have text containing e-mail addresses and you need to extract them out of the text. You do not know what these addresses are, so you cannot list them.

However each e-mail address is issued according to the following rules: it can start with any alphanumeric symbol, the further characters can be alphanumeric, underscore, dot or dash, then it should contain the @ symbol that separates the domain. The domain can also hold any alphanumeric characters, underscores, dots or dashes, but the final dot should be followed by a sequence of letters from two to six characters length. By applying the regular expressions syntax notation we can compose the pattern: \w+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,6} that specifies an e-mail address.

The sample below seeks and extracts several e-mail addresses from an input string using this pattern.

JavaScript, JScript

function ExtractDemo()
{
var regEx, Matches, i;
var ResStr = ""
var InStr = "Please e-mail us at [email protected] or [email protected]";
  // Set regular expression pattern that specifies an e-mail address
  regEx = /\w+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,6}/ig;
  // Perform search operation
  Matches = InStr.match(regEx);
  // Iterate through Matches array
  for (i=0; i<Matches.length; i++)
    {
    ResStr=ResStr+Matches[i]+"; ";
    }

  Log.Message("These e-mails were extracted: "+ResStr);
  // Posts the following to the log:
  // "These e-mails were extracted: [email protected]; [email protected]; "
}

Python

import re
def ExtractDemo():
  InStr = "Please e-mail us at [email protected] or [email protected]"
  # Set a regular expression pattern that specifies an e-mail address
  regEx = "(\w+@[\w-]+\.+[a-zA-Z]{2,6})"
  # Perform search operation
  Matches = re.findall(regEx, InStr)
  # Iterate through Matches array
  if Matches:
    for i in Matches:
      ResStr=ResStr + str(i) + "; "

    Log.Message("These e-mails were extracted: "+ResStr);
    # Posts the following to the log:
    # "These e-mails were extracted: [email protected]; [email protected]; "

VBScript

Sub ExtractDemo
Dim InStr, regEx, Match, Matches
  InStr = "Please e-mail us at [email protected] or [email protected]"
  ' Create a regular expression
  Set regEx = New RegExp
  ' Set regular expression pattern that specifies an e-mail address
  regEx.Pattern = "\w+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,6}"
  regEx.Global = True ' Set global applicability
  ' Perform search operation
  Set Matches = regEx.Execute(InStr)
  ' Iterate Matches collection
  For Each Match in Matches
    ResStr = ResStr & Match.Value & "; "
  Next

  Log.Message("These e-mails were extracted: "+ResStr)
  ' Posts the following to the log:
  ' "These e-mails were extracted: [email protected]; [email protected]; "
End Sub

DelphiScript

procedure ExtractDemo;
var
  regEx: OleVariant;
  InStr, ResStr: String;
begin
  ResStr:='';
  InStr := 'Please e-mail us at [email protected] or [email protected]';
  // Create a regular expression
  regEx:=HISUtils.RegExpr;
  // Set regular expression pattern that specifies an e-mail address
  regEx.Expression:='\w+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,6}';
  // Execute search
  if regEx.Exec(InStr) then
  Repeat
    ResStr := ResStr + regEx.Match[0] + '; ';
  Until not regEx.ExecNext;

  Log.Message('These e-mails were extracted: '+ResStr);
  // Posts the following to the log:
  // 'These e-mails were extracted: [email protected]; [email protected]; '
end;

C++Script, C#Script

function ExtractDemo()
{
var regEx, Matches;
var ResStr = ""
var InStr = "Please e-mail us at [email protected] or [email protected]";
  // Set regular expression pattern that specifies an e-mail address
  regEx = /\w+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,6}/ig;
  // Perform search operation
  Matches = InStr["match"](regEx);
  // Iterate through Matches array
  for (var i=0; i<Matches["length"]; i++)
  {
    ResStr=ResStr+Matches[i]+"; ";
  }

  Log["Message"]("These e-mails were extracted: "+ResStr);
  // Posts the following to the log:
  // "These e-mails were extracted: [email protected]; [email protected]; "
}

Replace text fragments

Another frequent operation with regular expressions is replacement. It works in the same way as the search operation, but the found occurrences are substituted with the specified text. The example below locates an e-mail address within an input string and replaces it with the specified string.

JavaScript, JScript

function ReplaceDemo()
{
var regEx;
var ResStr = ""
var InStr = "If you have any questions, send them to [email protected].";
  // Set regular expression pattern that specifies an e-mail address
  regEx = /\w+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,6}/ig;
  // Perform replace operation
  ResStr = InStr.replace(regEx, "mycompany support team" );

  Log.Message(ResStr);
  // Posts the following to the log:
  // "If you have any questions, send them to mycompany support team."
}

Python

import re
def ReplaceDemo():
  InStr = "If you have any questions, send them to [email protected].";
  # Set regular expression pattern that specifies an e-mail address
  regEx = "(\w+@[\w-]+\.+[a-zA-Z]{2,6})"
  # Perform replace operation
  ResStr = re.sub(regEx,"mycompany support team", InStr)

  Log.Message(ResStr);
  # Posts the following to the log:
  # "If you have any questions, send them to mycompany support team."

VBScript

Sub ReplaceDemo
Dim InStr, regEx, Match, Matches
  InStr = "If you have any questions, send them to [email protected]."
  ' Create a regular expression
  Set regEx = New RegExp
  ' Set regular expression pattern that specifies an e-mail address
  regEx.Pattern = "\w+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,6}"
  regEx.Global = True ' Set global applicability
  ' Perform replace operation
  ResStr = regEx.Replace(InStr, "mycompany support team")

  Log.Message(ResStr)
  ' Posts the following to the log:
  ' "If you have any questions, send them to mycompany support team."
End Sub

DelphiScript

procedure ReplaceDemo;
var
  regEx: OleVariant;
  InStr, ResStr: String;
begin
  ResStr:='';
  InStr := 'If you have any questions, send them to [email protected].';
  // Create a regular expression
  regEx:=HISUtils.RegExpr;
  // Set regular expression pattern that specifies an e-mail address
  regEx.Expression:='\w+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,6}';
  // Perform replace operation
  ResStr := regEx.Replace(InStr,'mycompany support team');

  Log.Message(ResStr);
  // Posts the following to the log:
  // 'If you have any questions, send them to mycompany support team.'
end;

C++Script, C#Script

function ReplaceDemo()
{
var regEx;
var ResStr = ""
var InStr = "If you have any questions, send them to [email protected].";
  // Set regular expression pattern that specifies an e-mail address
  regEx = /\w+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,6}/ig;
  // Perform replace operation
  ResStr = InStr["replace"](regEx, "mycompany support team" );

  Log["Message"](ResStr);
  // Posts the following to the log:
  // "If you have any questions, send them to mycompany support team."
}

Specify variable parts in object captions

You can use regular expressions to specify variable parts in object captions in calls to methods that simulate user actions (ClickItem, SelectItem, CheckItem, ClickCell, and so forth). To recognize a control, TestComplete often uses its caption. Captions of some controls may change from one application run to another, so using regular expressions makes the created tests stable.

The following code demonstrates how you can use regular expressions to select a combo-box item which contains "sans" in its caption:

JavaScript, JScript

function VariablePartsDemo()
{
  var notepad;
  var dlgFont;
  TestedApps.notepad.Run();
  notepad = Sys.Process("notepad");
  notepad.Window("Notepad", "*").MainMenu.Click("Format|Font...");
  dlgFont = notepad.Window("#32770", "Font");
  // Use a regular expression pattern that matches any sans serif typeface
  dlgFont.Window("ComboBox", "", 1).ClickItem(/.* [S|s]ans/);
}

Python

import re
def VariablePartsDemo():
  TestedApps.notepad.Run()
  notepad = Sys.Process("notepad")
  notepad.Window("Notepad", "*").MainMenu.Click("Format|Font...")
  dlgFont = notepad.Window("#32770", "Font")
  # Use a regular expression pattern that matches any sans serif typeface
  dlgFont.Window("ComboBox", "", 1).ClickItem(re.compile(".* [S|s]ans"))

VBScript

Sub VariablePartsDemo
  Dim notepad
  Dim dlgFont
  Dim regEx
  TestedApps.notepad.Run
  Set notepad = Sys.Process("notepad")
  Call notepad.Window("Notepad", "*").MainMenu.Click("Format|Font...")
  Set dlgFont =notepad.Window("#32770", "Font")
  ' Create a regular expression
  Set regEx = New RegExp
  ' Set a regular expression pattern that matches any sans serif typeface
  regEx.Pattern = ".* [S|s]ans"
  Call dlgFont.Window("ComboBox", "", 1).ClickItem(regEx)
End Sub

DelphiScript

procedure VariablePartsDemo;
  var notepad : OleVariant;
  var dlgFont : OleVariant;
  var regEx: OleVariant;
begin
  TestedApps.notepad.Run;
  notepad := Sys.Process('notepad');
  notepad.Window('Notepad', '*').MainMenu.Click('Format|Font...');
  dlgFont := notepad.Window('#32770', 'Font');
   // Create a regular expression
  regEx:=HISUtils.RegExpr;
  // Set a regular expression pattern that matches any sans serif typeface
  regEx.Expression:='.* [S|s]ans';
  dlgFont.Window('ComboBox', '', 1).ClickItem(regEx);
end;

C++Script, C#Script

function VariablePartsDemo()
{
  var notepad;
  var dlgFont;
  TestedApps["notepad"]["Run"]();
  notepad = Sys["Process"]("notepad");
  notepad["Window"]("Notepad", "*")["MainMenu"]["Click"]("Format|Font...");
  dlgFont = notepad["Window"]("#32770", "Font");
  // Use a regular expression pattern that matches any sans serif typeface
  dlgFont["Window"]("ComboBox", "", 1)["ClickItem"](/.* [S|s]ans/);
}

Another example of using regular expressions in object captions is testing localized or multilingual applications. When the control's caption can have several variants in different languages, you may use regular expressions to list all possible variants of the caption.

The following code shows how to identify a grid cell using the column's caption in English, German, French, and Spanish locales:

JavaScript, JScript

function LocaleIndependentDemo()
{
  var dataGridView;
  var dataGridViewTextBoxEditingControl;
  var caption;
  TestedApps.DataGridViewSample.Run();
  dataGridView = Sys.Process("DataGridViewSample").WinFormsObject("Form1").WinFormsObject("dataGridView1");
  // Set a regular expression pattern that specifies a caption in multiple languages
  caption = /(Customer Name)|(Kundenname)|(Nom du client)|(Nombre del cliente)/
  dataGridView.ClickCell(3, caption);
}

Python

import re
def LocaleIndependentDemo():
  TestedApps.DataGridViewSample.Run()
  dataGridView = Sys.Process("DataGridViewSample").WinFormsObject("Form1").WinFormsObject("dataGridView1")
  # Set regular expression pattern that specifies caption in multiple languages 
  caption = re.compile("(Customer Name)|(Kundenname)|(Nom du client)|(Nombre del cliente)")
  dataGridView.ClickCell(3, caption)

VBScript

Sub LocaleIndependentDemo
  Dim form1
  Dim dataGridView
  Dim caption
  TestedApps.DataGridViewSample.Run
  Set form1 = Sys.Process("DataGridViewSample").WinFormsObject("Form1")
  Set dataGridView = form1.WinFormsObject("dataGridView1")
  ' Create a regular expression
  Set caption = New RegExp
  ' Set a regular expression pattern that specifies a caption in multiple languages
  caption.Pattern = "(Customer Name)|(Kundenname)|(Nom du client)|(Nombre del cliente)"
  Call dataGridView.ClickCell(3, caption)
End Sub

DelphiScript

procedure LocaleIndependentDemo;
  var dataGridView : OleVariant;
  var caption : OleVariant;
begin
  TestedApps.DataGridViewSample.Run;
  dataGridView := Sys.Process('DataGridViewSample').WinFormsObject('Form1').WinFormsObject('dataGridView1');
  // Create a regular expression
  caption:=HISUtils.RegExpr;
  // Set a regular expression pattern that specifies a caption in multiple languages
  caption.Expression := '(Customer Name)|(Kundenname)|(Nom du client)|(Nombre del cliente)';
  dataGridView.ClickCell(3, caption);
end;

C++Script, C#Script

function LocaleIndependentDemo()
{
  var dataGridView;
  var dataGridViewTextBoxEditingControl;
  var caption;
  TestedApps["DataGridViewSample"]["Run"]();
  dataGridView = Sys["Process"]("DataGridViewSample")["WinFormsObject"]("Form1")["WinFormsObject"]("dataGridView1");
  // Set a regular expression pattern that specifies a caption in multiple languages
  caption = /(Customer Name)|(Kundenname)|(Nom du client)|(Nombre del cliente)/
  dataGridView["ClickCell"](3, caption);
}

Replace multiple lines of text

With regular expressions you can define patterns that contain multiple lines of text. That is a pattern can include special characters like carriage returns and line feeds. This could be useful when handling large fragments of formatted text, like text files or code listings. Note, that tokens for special symbols in regular expression patterns do not depend on a scripting language. However, to specify a replacement string that would hold multiple text lines, you should use language-specific notation of special characters. (See Special characters section in Working With Strings topic that corresponds to the language you use.) Here is a sample routine that accepts Pascal code fragments and inserts a comment before a function declaration, like in the fragment below:

Before:

function Foo(Param1);
begin
    ...
end;
 

After:

//A helper routine
function Foo(Param1);
begin
    ...
end;

JavaScript, JScript

function InsertComments(ProgramText)
{
var NewProgramText, ReplacementLines;
var regEx;
  // Set regular expression pattern that corresponds to Pascal function declaration
  regEx =/;(\r\n)+function/g;
  ReplacementLines = ";\r\n\r\n//A helper routine\r\nfunction";
  // Perform replace operation
  NewProgramText = ProgramText.replace(regEx, ReplacementLines);
  Log.Message("Comments have been inserted. See Remarks section for the new text of a program.", NewProgramText);
  return NewProgramText;
};

Python

import re
def InsertComments(ProgramText):
  # Set regular expression pattern that corresponds to Pascal function declaration
  regEx ="(\r\n+function)"
  ReplacementLines = ";\r\n\r\n//A helper routine\r\nfunction";
  # Perform replace operation
  NewProgramText = re.sub(regEx, ReplacementLines, ProgramText)
  Log.Message("Comments have been inserted. See Remarks section for the new text of a program.", NewProgramText)
  return NewProgramText

VBScript

Function InsertComments(ProgramText)
Dim NewProgramText, ReplacementLines, regEx
  ' Create a regular expression
  Set regEx = New RegExp
  ' Set regular expression pattern that corresponds to Pascal function declaration
  regEx.Pattern = ";(\r\n)+function"
  regEx.Global = True ' Set global applicability
  ReplacementLines = ";"& vbNewLine & vbNewLine & "//A helper routine" & vbNewLine & "function"
  ' Perform replace operation
  NewProgramText = regEx.Replace(ProgramText, ReplacementLines)
  Call Log.Message("Comments have been inserted. See Remarks section for the new text of a program.", NewProgramText)
  InsertComments = NewProgramText
End Function

DelphiScript

function InsertComments(ProgramText);
var NewProgramText, ReplacementLines: string;
  regEx: OleVariant;
begin
  // Create a regular expression
  regEx := HISUtils.RegExpr;
  // Set regular expression pattern that corresponds to Pascal function declaration
  regEx.Expression:=';(\r\n)+function';
  ReplacementLines := ';'+#10#13+'//A helper routine'+#10#13+'function';
  // Perform replace operation
  NewProgramText := regEx.Replace(ProgramText, ReplacementLines);
  Log.Message('Comments have been inserted. See Remarks section for the new text of a program.', NewProgramText);
  Result:=NewProgramText;
end;

C++Script, C#Script

function InsertComments(ProgramText)
{
var NewProgramText, ReplacementLines;
var regEx;
  // Set regular expression pattern that corresponds to Pascal function declaration
  regEx =/;(\r\n)+function/g;
  ReplacementLines = ";\r\n\r\n//A helper routine\r\nfunction";
  // Perform replace operation
  NewProgramText = ProgramText["replace"](regEx, ReplacementLines);
  Log["Message"]("Comments have been inserted. See Remarks section for the new text of a program.", NewProgramText);
  return NewProgramText;
};

Split found text into submatches

This sample demonstrates how to address submatches of the found text. To do this, you should define sub-expressions beforehand. Sub-expressions are denoted in a pattern with the parenthesis. The code below extracts the phone number from an input string, parses the number into sub-group elements and forms a new string that contains those elements.

JavaScript, JScript

function SubMatchesDemo()
{
var regEx, Matches;
var ResStr = ""
var InStr = "Jack, when you arrive, phone me +1(234)567-89-01.";
  // Set regular expression pattern that specifies a phone number
  regEx = /(\+\d *)?(\(\d+\) *)?(\d+(-\d*)*)/g;
  // Perform search operation
  Matches = InStr.match(regEx);
  // Access to the whole match and its submatches
  ResStr = "The phone number " + RegExp.lastMatch + " consists of these parts:\r\n"
  ResStr = ResStr + "Zone code " + RegExp.$1 + ", "
  ResStr = ResStr + "city code " + RegExp.$2 + " "
  ResStr = ResStr + "and number itself " + RegExp.$3 + "."
  
  Log.Message(ResStr)
  // Posts the following to the log:
  // 'The phone number +1(234)567-89-01 consists of these parts:
  // Zone code +1, city code (234) and number itself 567-89-01.'
}

Python

def SubMatchesDemo():
  InStr = "Jack, when you arrive, phone me +1(234)567-89-01."
  # Set regular expression pattern that specifies a phone number
  regEx = "(\+\d *?)(\(\d+\) *?)(\d+(-\d*)*)"
  # Perform search operation
  Matches = re.search(regEx, InStr)
  # Access to the whole match and its submatches
  ResStr = "The phone number " + Matches.group(0) + " consists of these parts:\r\n"
  ResStr = ResStr + "Zone code " + Matches.group(1) + ", "
  ResStr = ResStr + "city code " + Matches.group(2) + " " 
  ResStr = ResStr + "and number itself " + Matches.group(3) + "."
  
  Log.Message(ResStr)
  # Posts the following to the log:
  # 'The phone number +1(234)567-89-01 consists of these parts:
  # Zone code +1, city code (234) and number itself 567-89-01.'

VBScript

Sub SubMatchesDemo
Dim InStr, regEx, Match, Matches
  InStr = "Jack, when you arrive, phone me +1(234)567-89-01."
  ' Create a regular expression
  Set regEx = New RegExp
  ' Set regular expression pattern that specifies a phone number
  regEx.Pattern = "(\+\d *)?(\(\d+\) *)?(\d+(-\d*)*)"
  ' Perform search operation
  Set Matches = regEx.Execute(InStr)
  ' Access to the whole match and its submatches
  ResStr = "The phone number " & Matches(0).Value & " consists of these parts:" &vbNewLine
  ResStr = ResStr & "Zone code " & Matches(0).SubMatches(0) & ", "
  ResStr = ResStr & "city code " & Matches(0).SubMatches(1) & " "
  ResStr = ResStr & "and number itself " & Matches(0).SubMatches(2) & "."

  Log.Message(ResStr)
  ' Posts the following to the log:
  ' 'The phone number +1(234)567-89-01 consists of these parts:
  ' Zone code +1, city code (234) and number itself 567-89-01.
End Sub

DelphiScript

procedure SubMatchesDemo;
var regEx: OleVariant;
    InStr, ResStr: String;
begin
  ResStr:='';
  InStr := 'Jack, when you arrive, phone me +1(234)567-89-01.';
  // Create a regular expression
  regEx:=HISUtils.RegExpr;
  // Set regular expression pattern that specifies a phone number
  regEx.Expression:='(\+\d *)?(\(\d+\) *)?(\d+(-\d*)*)';
  // Perform search operation
  if regEx.Exec(InStr) then
    // Access to the whole match and its submatches
    ResStr := regEx.Substitute('The phone number $& consists of these parts:'+#13#10
    +'Zone code $1, city code $2 and number itself $3.');
  Log.Message(ResStr);
  // Posts the following to the log:
  // 'The phone number +1(234)567-89-01 consists of these parts:
  // Zone code +1, city code (234) and number itself 567-89-01.'
end;

C++Script, C#Script

function SubMatchesDemo()
{
var regEx, Matches;
var ResStr = ""
var InStr = "Jack, when you arrive, phone me +1(234)567-89-01.";
  // Set regular expression pattern that specifies a phone number
  regEx = /(\+\d *)?(\(\d+\) *)?(\d+(-\d*)*)/g;
  // Perform search operation
  Matches = InStr["match"](regEx);
  // Access to the whole match and its submatches
  ResStr = "The phone number " + RegExp["lastMatch"] + " consists of these parts:\r\n"
  ResStr = ResStr + "Zone code " + RegExp.$1 + ", "
  ResStr = ResStr + "city code " + RegExp.$2 + " "
  ResStr = ResStr + "and number itself " + RegExp.$3 + "."
    
  Log["Message"](ResStr)
  // Posts the following to the log:
  // 'The phone number +1(234)567-89-01 consists of these parts:
  // Zone code +1, city code (234) and number itself 567-89-01.'
}

See Also

Regular Expressions Syntax
RegExpr Object

Highlight search results