Using Regular Expressions in Scripts

Applies to TestComplete 14.71, last modified on April 22, 2021

Regular expressions are a versatile and convenient way of searching, replacing, extracting and validating data in text. At a first glance they may remind you of wildcards; however, using regular expressions you can define more distinctive patterns, rather than just any character or any sequence of characters.

A regular expression is a special text string that describes a search pattern. The pattern defines one or more substrings to match within a text fragment. The regular expression serves as a template for matching a character pattern to the string that is being searched. Regular expressions consist of literal symbols and special combinations of characters, named tokens, that denote non-printable characters, symbols of a particular type (for instance, digits or alphanumeric characters) and regular expression engine’s instructions. The Regular Expressions Syntax topic contains a list of regular expression tokens that are applied in TestComplete.

A remarkable feature of regular expressions is that you can refer not only to the entire match found, but to its submatches as well. The submatches are those text fragments that correspond to sub-expressions that form a pattern. The sub-expressions are defined in a pattern using parenthesis. The sequence of characters and tokens written within a parenthesis is considered as sub-expression. For instance, the (\d{3})(\w{5}) pattern has two sub-expressions: first one denoting any sequence of three digits and the second denotes a sequence of five alphanumeric characters.

TestComplete supports native regular expressions (the ones that are built into scripting language engines) and non-native regular expressions (its own regular expressions). In different cases, you can use different regular expressions:

  • When working with text (when searching for text, replacing it, dividing it into several parts) - use native regular expressions.

  • To specify variable parts of a string property value in FindXXX methods (Find, FindChild, FindEx, FindAll and so on) and in the WaitProperty method - use non-native regular expressions.

  • To specify variable parts of item captions when using ClickItem, SelectItem, wItem and similar methods, you can use both native and non-native regular expressions.

This topic explains how to use both types of regular expressions and provides you with some examples.

Non-Native Regular Expressions in TestComplete

To create regular expressions in a method, you pass it as the corresponding string parameter with the regexp: prefix:

obj.Find("WndCaption", "regexp:pattern");

The syntax of non-native regular expressions does not depend on the scripting language you use. You can find a full syntax reference in the Regular Expression Syntax topic.

Currently, you cannot use regular expressions to specify a subitem to interact with (for example, you cannot use them in the Click method of the Menu control).

The examples below demonstrate how you can use regular expressions to find a specific control or its child and to perform a click on the specified item.

JavaScript, JScript

function main()
{
  var notepad, window, dialog, control;
  WshShell.Run("notepad.exe", SW_NORMAL);
  notepad = Sys.Process("notepad");
  
  // Get Notepad's main window by using a regular expression
  window = notepad.Find("WndCaption", "regexp:.* Notepad", 5);
  
  window.MainMenu.Click("Format|Font...");
  dialog = notepad.Find("WndCaption", "Font");
  
  // Specify words that must be in the specified property
  // of the desired object by using a regular expression
  control = dialog.FindChild("wItemList", "regexp:.*Western.*Arabic.*", 5);
  
  // Specify the item to be clicked using a regular expression
  control.ClickItem("regexp:.*European$");
}

Python

def main():
  WshShell.Run("notepad.exe", SW_NORMAL);
  notepad = Sys.Process("notepad");
  
  # Get Notepad's main window by using a regular expression
  window = notepad.Find("WndCaption", "regexp:.* Notepad");
  
  window.MainMenu.Click("Format|Font...");
  dialog = notepad.Find("WndCaption", "Font");
  
  # Specify words that must be in the specified property
  # of the desired object by using a regular expression
  control = dialog.FindChild("wItemList", "regexp:.*Western.*Arabic.*", 2);
  
  # Specify the item to be clicked using a regular expression
  control.ClickItem("regexp:.*European$");

VBScript

Sub main()
 Dim notepad, window, dialog, control
  Call WshShell.Run("notepad.exe", SW_NORMAL)
 Set notepad = Sys.Process("notepad")
   
 ' Get Notepad's main window by using a regular expression
 Set window = notepad.Find("WndCaption", "regexp:.* Notepad", 5)
   
 window.MainMenu.Click("Format|Font...")
 Set dialog = notepad.Find("WndCaption", "Font")
   
 ' Specify words that must be in the specified property
 ' of the desired object by using a regular expression
 Set control = dialog.FindChild("wItemList", "regexp:.*Western.*Arabic.*", 5)
   
 ' Specify the item to be clicked using a regular expression
 control.ClickItem("regexp:.*European$")
End Sub

DelphiScript

procedure main();
var notepad, window, dialog, control;
begin
  WshShell.Run('notepad.exe', SW_NORMAL);
  notepad := Sys.Process('notepad');
   
  // Get Notepad's main window by using a regular expression
  window := notepad.Find('WndCaption', 'regexp:.* Notepad', 5);
   
  window.MainMenu.Click('Format|Font...');
  dialog := notepad.Find('WndCaption', 'Font');
   
  // Specify words that must be in the specified property
  // of the desired object by using a regular expression
  control := dialog.FindChild('wItemList', 'regexp:.*Western.*Arabic.*', 5);
   
  // Specify the item to be clicked using a regular expression
  control.ClickItem('regexp:.*European$');
end;

C++Script, C#Script

function main()
{
  var notepad, window, dialog, control
  WshShell["Run"]("notepad.exe", SW_NORMAL);
  notepad = Sys["Process"]("notepad");
   
  // Get Notepad's main window by using a regular expression
  window = notepad["Find"]("WndCaption", "regexp:.* Notepad", 5);
   
  window["MainMenu"].Click("Format|Font...");
  dialog = notepad["Find"]("WndCaption", "Font");
   
  // Specify words that must be in the specified property
  // of the desired object by using a regular expression
  control = dialog["FindChild"]("wItemList", "regexp:.*Western.*Arabic.*", 5);
   
  // Specify the item to be clicked using a regular expression
  control["ClickItem"]("regexp:.*European$");
}

Native Regular Expressions in Scripting Languages

In different scripting languages, regular expressions are supported in different ways.

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.

Practical Tasks

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

Searching 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]; "
}

Replacing 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."
}

Specifying 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);
}

Replacing 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;
};

Dividing 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