随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253

//匹配右边
var
  reg: TPerlRegEx;
begin
  reg := TPerlRegEx.Create(nil);

  reg.Subject := 'Delphi 6; Delphi 7; Delphi 2007; Delphi Net';
  reg.RegEx   := 'Delphi (?=2007)'; // ?=
  reg.Replacement := '◆';
  reg.ReplaceAll;

  ShowMessage(reg.Subject); //返回: Delphi 6; Delphi 7; ◆2007; Delphi Net

  FreeAndNil(reg);
end;

//不匹配右边 var reg: TPerlRegEx; begin reg := TPerlRegEx.Create(nil); reg.Subject := 'Delphi 6; Delphi 7; Delphi 2007; Delphi Net'; reg.RegEx := 'Delphi (?!2007)'; // ?! reg.Replacement := '◆'; reg.ReplaceAll; ShowMessage(reg.Subject); //返回: ◆6; ◆7; Delphi 2007; ◆Net FreeAndNil(reg); end;
//匹配左边 var reg: TPerlRegEx; begin reg := TPerlRegEx.Create(nil); reg.Subject := '111, 222, ¥333, ¥444'; reg.RegEx := '(?<=¥)\d{3}'; // ?<= reg.Replacement := '◆'; reg.ReplaceAll; ShowMessage(reg.Subject); //返回: 111, 222, ¥◆, ¥◆ FreeAndNil(reg); end;
//不匹配左边 var reg: TPerlRegEx; begin reg := TPerlRegEx.Create(nil); reg.Subject := '111, 222, ¥333, ¥444'; reg.RegEx := '(?<!¥)\d{3}'; // ?<! reg.Replacement := '◆'; reg.ReplaceAll; ShowMessage(reg.Subject); //返回: ◆, ◆, ¥333, ¥444 FreeAndNil(reg); end;
posted on 2007-12-21 16:43  万一  阅读(...)  评论(...编辑  收藏