009.EscapeRegExChars

类型:function

可见性:public

所在单元:RegularExpressionsCore

父类:TPerlRegEx

 

把转义字符变成原意字符

例如\d意为0~9某个数字,通过此函数转换后则变成\d字符,不存在转义,效果和\\d一样

 

测试代码:

program Project1;

{$APPTYPE CONSOLE}

uses
    System.SysUtils, System.RegularExpressionsCore;

var
    regular: TPerlRegEx;

begin
    try
        //使用EscapeRegExChars将+变成普通字符,不在有匹配后面多个的效果
        regular := TPerlRegEx.Create;
        regular.Subject := 'This a+b b+c test \d str';
        regular.RegEx := regular.EscapeRegExChars('a+b');
        regular.Replacement := '*';
        regular.ReplaceAll;
        Writeln(regular.Subject);

        //直接用\将+号变成普通字符,和使用 EscapeRegExChars转换后效果一样
        regular.RegEx := 'b\+c';
        regular.Replacement := '#';
        regular.ReplaceAll;
        Writeln(regular.Subject);

        //将\d变成普通字符替换掉
        regular.RegEx := regular.EscapeRegExChars('\d');
        regular.Replacement := '?';
        regular.ReplaceAll;
        Writeln(regular.Subject);
        Readln;
        FreeAndNil(regular);
        { TODO -oUser -cConsole Main : Insert code here }
    except
        on E: Exception do
            Writeln(E.ClassName, ': ', E.Message);
    end;

end.

结果:

posted @ 2014-04-20 20:23  oszhouzhb  阅读(204)  评论(0编辑  收藏  举报