SubExprMatchCount Property

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

Description

Property Value

The integer number of found sub-expressions. See details in the remarks section.

Declaration

RegExprObj.SubExprMatchCount

Read-Only Property Integer
RegExprObj An expression, variable or parameter that specifies a reference to a RegExpr object

Applies To

The property is applied to the following object:

Property Value

Remarks

If an expression was found and some of its sub-expressions matched, then SubExprMatchCount returns the number of sub-expressions found. To get the position, length and text of a matched sub-expression, use the RegExpr.MatchPos, RegExpr.MatchLen and RegExpr.Match properties correspondingly. For those sub-expressions that were not found these properties return: MatchPos = -1; MatchLen = -1; Match = '';.

If a whole expression was found, but no sub-expressions matched, then SubExprMatchCount returns zero.

If an expression was not found at all, then SubExprMatchCount returns -1.

Example

Suppose we have the following expression: Expression := '(1)?2(3)?';

Let us consider the SubExprMatchCount values for the different input texts:

Input text Exec result Property value Matches
Exec ('123') True SubExprMatchCount=2 Match[0]='123'; Match[1]='1'; Match[2]='3'
Exec ('12') True SubExprMatchCount=1 Match[0]='12'; Match[1]='1'
Exec ('23') True SubExprMatchCount=2 Match[0]='23'; Match[1]=''; Match[2]='3'
Exec ('2') True SubExprMatchCount=0 Match[0]='2'
Exec ('7') False SubExprMatchCount=-1 Match[0]=''

The following sample demonstrates how to use the RegExpr.SubExprMatchCount property to obtain the number of sub-expressions found in the input text.

DelphiScript

procedure MatchSample;

var
  MyRegExpr : OleVariant;
  Input, Expr, ResStr: string;
  MatchCount, i: integer;

begin
  MyRegExpr := HISUtils.RegExpr;
  Input : = 'The ProjectSuite.tcCfgExtender file stores the project suite settings.';
  Expr := '([\w]+)(\.[\w]+)';
  MyRegExpr.Expression : = Expr;
  MyRegExpr.Exec(Input);
  MatchCount := MyRegExpr.SubExprMatchCount;

  if MatchCount >= 0 then
    begin
      for i := 0 to MatchCount do
        begin
          ResStr := 'Match[' + aqConvert.IntToStr(i) + '] = ''' + MyRegExpr.Match[i] + '''';
          Log.Message(ResStr);
        end;
    end;

end;

See Also

Regular Expressions Syntax
Using Regular Expressions in Scripts
Exec Method
ModifierStr Property
Match Property
MatchLen Property
MatchPos Property

Highlight search results