ScriptCore3.RegExprSubExpressions

From Soldat Community Wiki
Jump to: navigation, search
function RegExprSubExpressions(const ARegExpr: string; ASubExprs: TStrings; AExtendedSyntax: boolean): integer
 ARegExpr: Regular expression to be analysed
 ASubExprs: list of subexpressions found in ARegExpr
 AExtendedSyntax: must be True if modifier /m will be On while using the r.e.
 Result: used to indicate if there are missing brackets in ARegExpr

Description

Makes list of subexpressions found in ARegExpr r.e.

  • If Result <> 0, then ASubExpr can contain empty items or illegal ones
n Value Description
0 Success. No unbalanced brackets was found
-1 There are not enough closing brackets ')'
-(n+1) At position n was found opening '[' without corresponding closing ']';
n At position n was found closing bracket ')' without corresponding opening '('.

For detailed information about Regular Expressions see Regular_Expression on Wikipedia

Example

([A-Z]* [a-z]*) (ab[c]{1,2})*
-->
Result = 0
ASubExprs:
  ([A-Z]* [a-z]*) (ab[c]{1,2})*
  [A-Z]* [a-z]*
  ab[c]{1,2}

Testing script

var
    reg: string;
    str: TStringList;

procedure TestInit(newReg: string);
begin
    reg := newReg;
    str := File.CreateStringList;
    WriteLn('-- RegExprSubExpressions Test Start --');
    WriteLn('');
end;

procedure TestEnd();
begin
    WriteLn('-- RegExprSubExpressions Test Stop --');
    str.Free;
end;
    
procedure RegExprChanged(AShowErrorPos: boolean);
var
    i : integer;
    n : integer;
    s : string;
begin
    n := RegExprSubExpressions(reg,str,False);
    case n of //###0.942
        0: WriteLn('No errors');
        -1: WriteLn('Not enough ")"');
        else 
        begin
            if n < 0 then
            begin
                s := 'No "]" found for "["';
                n := (-n) - 1;
            end
            else 
                s := 'Unexpected ")"';
            if AShowErrorPos then 
                s := s + ' at pos ' + IntToStr(n);
            WriteLn(s);
        end;
    end;
    with str do
        for i:=0 to Count-1 do
            Strings[i] := '$' + IntToStr(i) + ': ' + Strings[i];
    WriteLn(str.Text);
end;

begin
    TestInit('([A-Z]* [a-z]*) (ab[c]{1,2})*');
    RegExprChanged(TRUE);
    TestEnd();
end.