delphi正则应用

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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值