您的位置:首页 > 编程语言 > Delphi

delphi正则应用

2016-07-21 14:29 537 查看
function TrackMatchs(strText: string;

                     strExp : string;

                     Matchs : TStrings=nil;

                     bMS    : Boolean=false;

                     bMG    : Boolean=false;

                     bMR    : Boolean=false): integer;

var

    i: integer;
RegExpr: TRegExpr;

begin

    result:= 0;

    if strExp='' then exit;

    RegExpr:= TRegExpr.Create;

    RegExpr.ModifierS:= bMS;

    RegExpr.ModifierG:= bMG;

    RegExpr.ModifierR:= bMR;

    RegExpr.ModifierI:= true; //不区分大小写

    RegExpr.Expression:= strExp;

try
RegExpr.Expression := strExp;
if RegExpr.Exec (strText) then

        begin

            REPEAT

                if Assigned(Matchs) then

                    Matchs.Add(RegExpr.Match[0]);

                Inc(result);

            UNTIL not RegExpr.ExecNext;

        end;
finally

        FreeAndNil(RegExpr);

  end;

end;

function GetMatchs(strText: string;

                   strExp : string;

                   Matchs : TStrings=nil;

                   bMS    : Boolean=true;

                   bMG    : Boolean=false;

                   bMR    : Boolean=false;

                   Ver    : byte=1;

                   iContent: integer=0): integer;

    

    //function GetMatchContent(strText: widestring; iContent, iMatchPos: integer): string;

    function GetMatchContent(strText: string; iContent, iMatchPos: integer; var line: integer): string;

    var

        v: string;

        istart, iend: integer;

    begin

        istart:= iMatchPos-(iContent div 2);

        if istart<1 then istart:=1;

        //iend:= iMatchPos+(iContent div 2);

        //if iend>length(strText) then iend:=length(strText);

        result:= copy(strText, istart, iContent);

        //2014-11-24 17:14 tig 输出所在行

        v:= copy(strText, 1, istart);

        line:= StrCount(#13#10, v);

        {

        result:= '';

        if iContent<length(strMatch) then exit;

        ipos:= pos(strMatch, strText);

        if ipos<1 then exit;

        istart:= ipos-(iContent div 2);

        if istart<1 then istart:=1;

        iend:= ipos+(iContent div 2);

        if iend>length(strText) then iend:=length(strText);

        result:= copy(strText, istart, iend-istart);

        }

    end;

var

    RegExpr: TRegExpr;

    i, nums, line: integer;

    sline, cont: string;

    DataStrs:  TStrings;

begin

    result:= 0;

    if strExp='' then exit;

    RegExpr  := TRegExpr.Create;

    DataStrs := TStringList.Create;

    try

        with  RegExpr do

        begin

            ModifierS  := bMS;

            ModifierG  := bMG;

            ModifierR  := bMR;

            ModifierI  := true; //不区分大小写

            Expression := strExp;

            if Exec(strText) then

            begin

                REPEAT

                    sline:= '';

                    if (Ver=2) or (Ver=3)  then

                    begin

                        nums:=0;

                        if Ver=2 then nums:= RegExpr.SubExprMatchCount;

                        cont:= '';

                        line:= -1;

                        for i:= 0 to nums do

                        begin

                            if iContent>0 then

                            begin

                                cont:= GetMatchContent(strText, iContent, RegExpr.MatchPos[i], line);

                                cont:=FastReplace(cont, #13#10, '', false);

                                cont:=FastReplace(cont, #09, '', false);

                            end;

                            sline:= sline+'<line>'+inttostr(line)+'</line>'+

                                          '<match>'+RegExpr.Match[i]+'</match>'+

                                          '<content>'+cont+'</content>'+#09

                        end;

                    end else

                    begin

                        sline:= RegExpr.Match[iContent];

                    end;

                    Inc(result);

                    DataStrs.Add(sline);

                UNTIL not ExecNext;

                if Assigned(Matchs) then  Matchs.Assign(DataStrs);

            end;

        end;

    finally

        RegExpr.Free;

        DataStrs.Free;

    end;

end;

function GetMatch(strText: string;

                  strExp : string;

                  idx    : integer=0;    //0表示

                  bMS    : Boolean=true;

                  bMG    : Boolean=false;

                  bMR    : Boolean=false): string;

var

    i, nums: integer;
RegExpr: TRegExpr;

    strLine, strContent: string;

begin

    result:= '';

    if strExp='' then exit;

    RegExpr:= TRegExpr.Create;

    RegExpr.ModifierS:= bMS;

    RegExpr.ModifierG:= bMG;

    RegExpr.ModifierR:= bMR;

    RegExpr.ModifierI:= true; //不区分大小写

    RegExpr.Expression:= strExp;

RegExpr.Expression:= strExp;
if RegExpr.Exec (strText) then
begin
   if idx>RegExpr.SubExprMatchCount then idx:= 0;
   result:= RegExpr.Match[idx];
end;

RegExpr.Free;

end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  正则