Repeat 'replace all' until 'Search string not found' occurs

Find, replace, find in files, replace in files, regular expressions

Repeat 'replace all' until 'Search string not found' occurs

Postby hbellens » Fri Jan 12, 2007 12:37 pm

Is it possible to repeat a 'replace all' untill 'Search string not found' occurs? (Macro?) And indicate a max number of iterations. (to avoid infinite loops)
User avatar
hbellens
Newbie
 
Posts: 2
Joined: Fri Jan 12, 2007 12:00 am

Re: Repeat 'replace all' until 'Search string not found' occurs

Postby mrainey56 » Fri Jan 12, 2007 1:56 pm

This basic structure?

Loop
Find "find string"
IfNotFound
ExitLoop
EndIf
Replace "replace string"
EndLoop
User avatar
mrainey56
Master
Master
 
Posts: 212
Joined: Tue Jul 27, 2004 11:00 pm
Location: Spartanburg, South Carolina

Re: Repeat 'replace all' until 'Search string not found' occurs

Postby Mofi » Fri Jan 12, 2007 2:35 pm

Hi mrainey56!

Your code will not be compiled by UltraEdit because a Replace command can be only immediately after a Find command. But following will work:

Loop x
Find "find string"
Replace All "replace string"
IfNotFound
ExitLoop
EndIf
EndLoop

I use this very often in my macros. Specify for x the maximum number of iterations. But most Replace All must be executed only once. My macros to delete all blank lines at Deleting blank lines using Perl regular expressions shows an exception where a Replace All in a Loop is really needed.

Just for your info: As DaveS detected, every Find in a macro is in real a Find with Replace with identical search and replace string. You can see that if you view a macro file in hex edit mode which contains only 1 Find but the find string is twice in the file. So IfFound and IfNotFound can be also used immediately after a Replace command because internally a Find only is in real also a Find with Replace.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4058
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Repeat 'replace all' until 'Search string not found' occurs

Postby hbellens » Fri Jan 12, 2007 2:55 pm

Guys,

thank you very much. Works fine!

But, regarding the speed at which you have replied with these answers (approx. 2 hours), one word: Amazing.

cu
User avatar
hbellens
Newbie
 
Posts: 2
Joined: Fri Jan 12, 2007 12:00 am

Re: Repeat 'replace all' until 'Search string not found' occurs

Postby mrainey56 » Fri Jan 12, 2007 2:56 pm

Thanks for the correction Mofi. That's what I get for not testing!
User avatar
mrainey56
Master
Master
 
Posts: 212
Joined: Tue Jul 27, 2004 11:00 pm
Location: Spartanburg, South Carolina


Return to Find/Replace/Regular Expressions