Exec Method

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

Description

The RegExpr.Exec method searches the InputStr for the nearest occurrence of text that matches the regular expression pattern specified by the Expression property. To get a portion of the text that matches the pattern, use the Match property.

Declaration

RegExprObj.Exec(InputStr)

RegExprObj An expression, variable or parameter that specifies a reference to a RegExpr object
InputStr [in]    Required    String    
Result Boolean

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

InputStr

Specifies the text that a regular expression is applied to.

Result Value

Returns True if the text matching the expression was found and False otherwise.

Remarks

You should initialize the Expression property with the regular expression before calling the Exec method, otherwise an error occurs.

A call to this method also assigns the value of the InputStr parameter to the InputString property.

This method has two versions: ExecNext and ExecPos that can be applied if both Expression and InputString have been initialized.

Example

This sample code illustrates how to use the regular expressions. It extracts e-mail addresses from the input string.

DelphiScript

procedure ExecSample;
var
  MyRegExp: OleVariant;
  ResStr: String;
begin
  MyRegExp:=HISUtils.RegExpr;
  ResStr:='';

  //This expression specifies an e-mail address pattern
  MyRegExp.Expression:='[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}';
  if MyRegExp.Exec('Please e-mail us at [email protected] or [email protected]') then
    Repeat
      ResStr := ResStr + MyRegExp.Match[0] + '; ';
    Until not MyRegExp.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;

See Also

Regular Expressions Syntax
Using Regular Expressions in Scripts
Expression Property
InputString Property
Match Property
ExecNext Method
ExecPos Method

Highlight search results