[yunqa.de] Re: RegEx replace

  • From: Delphi Inspiration <delphi@xxxxxxxx>
  • To: yunqa@xxxxxxxxxxxxx
  • Date: Sun, 08 Mar 2009 16:16:02 +0100

Serkan sertan wrote:

>how can I use regex.replace for wholeword and ignorecase
>
>can you example command as stringreplace ==> 
>memo1.text:=stringreplace(memo1.text, 'apple', 'orange');

{ RegExReplace function wrapper example.

  Visit the DIRegEx homepage for latest information and updates:

    http://www.yunqa.de/delphi/

  Copyright (c) 2009 Ralf Junker, The Delphi Inspiration <delphi@xxxxxxxx>

------------------------------------------------------------------------------ }

program DIRegEx_Replace_Whole_Word;

{$APPTYPE CONSOLE}
{$I DI.inc}

uses
  {$IFDEF FastMM}FastMM4, {$ENDIF}DISystemCompat, DIRegEx;

function RegExReplace(
  const AValue: RawByteString;
  const ASearch: RawByteString;
  const AReplace: RawByteString;
  const AOptions: TDIRegexCompileOptions): RawByteString;
var
  RE: TDIPerlRegEx;
begin
  RE := TDIPerlRegEx.Create(nil);
  try
    RE.SetSubjectStr(AValue);
    RE.CompileOptions := AOptions;
    RE.CompileMatchPatternStr(ASearch);
    RE.FormatPattern := AReplace;
    if RE.Replace2(Result) = 0 then
      Result := AValue;
  finally
    RE.Free;
  end;
end;

begin
  WriteLn(RegExReplace('Apple', '\bapple\b', 'orange', [coCaseLess]));
  WriteLn(RegExReplace('apple', '\bapple\b', 'orange', [coCaseLess]));

  WriteLn(RegExReplace('apples', '\bapple\b', 'orange', [coCaseLess]));

  WriteLn;
  WriteLn('Done - Press ENTER to Exit');
  ReadLn;
end.

Ralf 

_______________________________________________
Delphi Inspiration mailing list
yunqa@xxxxxxxxxxxxx
//www.freelists.org/list/yunqa



Other related posts: