Recent

Author Topic: Search & Replace with Regex one by one  (Read 1564 times)

Coldzer0

  • Jr. Member
  • **
  • Posts: 50
Search & Replace with Regex one by one
« on: November 08, 2018, 12:06:24 am »
Hello all

i wanted to make a code to replace all "{RNDCODE}'" in my .pas file , but i want to replace one by one

i don't want to replace all matches with the same new value i want to put new generated value to the every match

i try to do with this code but some times it didn't work as expected .

i'm using "RegExpr" unit

Code: Pascal  [Select][+][-]
  1.  
  2.   Source := TStringList.Create;
  3.   Source.LoadFromFile(filepath);
  4.   new := Source.Text;
  5.   re := TRegExpr.Create('{RNDCODE}');
  6.   try
  7.     while re.Exec(new) do
  8.     begin
  9.       if re.Match[0].Length > 0 then
  10.       begin
  11.         enc := RandomStuff;
  12.         Delete(new,re.MatchPos[0],re.MatchLen[0]);
  13.         Insert(enc,new,re.MatchPos[0]);
  14.       end;
  15.     end;
  16.   except on E: Exception do
  17.          WriteLn('Error : ',E.Message , ' -- ' , re.Match[0] , ' --- ', ExtractFileNameOnly(filepath));
  18.   end;
  19.  
  20.  


so if there's any better way to do it , please let me know

thanks

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: Search & Replace with Regex one by one
« Reply #1 on: November 08, 2018, 02:25:41 am »
i wanted to make a code to replace all "{RNDCODE}'" in my .pas file , but i want to replace one by one
Basic Regular Syntax (BRE) requires:
Code: Pascal  [Select][+][-]
  1. re := TRegExpr.Create('(\{REDCODE\})');...
Acceptable in this implementation Extended Regular Syntax (ERE):
Code: Pascal  [Select][+][-]
  1. re := TRegExpr.Create('({REDCODE})');...

 

TinyPortal © 2005-2018