How do I replace a particular sentence across multiple files?
-
Thanks a lot @guy038 - you made my efforts fruitful!
-
@guy038 said in How do I replace a particular sentence across multiple files?:
SEARCH (?s)Please.+?E-mail.+?treatment
REPLACE <b>$0</b>
I initially thought it would be that simple, but then considered the following (admittedly possibly not real-life examples):
Please E-mail .............. treatment (a <p........> string)Please E-mail ............treatment (a <p........> string)Please E-mail ............treatment Please mail ............eatment Please E-mail ............treatment Please E-mail ............reatment Please E-mail ............treatment
So the regex is forcing to be true, so it will expand until it DOES find the required text. So I was thinking of using
(?s)
and also.{1,5}
to limit the expansion. It does seem a bit rough but might be enough to limit the regex to ONLY what it should capture.Admittedly one thing that was lacking in the OP’s original request was what they wanted to do with it. The multiple scenarios with possible CR/LF’s in between (or not) complicates matters.
Terry
-
@Terry-R said in How do I replace a particular sentence across multiple files?:
The multiple scenarios with possible CR/LF’s in between (or not) complicates matters.
For what it’s worth, I continued down the road with my idea. This solution will prevent finding occurances of “Please” immediately followed by something which is NOT the text sought, but is followed by another occurance of the “Please…treatment” text to be found. As examples see:
Please E-mail .............. treatment (a <p........> string)Please E-mail ............treatment (a <p........> string)Please E-mail ............treatment Please find attached ............travel Please E-mail ............treatment Please E-mail ............find me Please E-mail ............treatment
Note there are 2 situations in the above example where “Please” is followed by something other than the text required. My solution will prevent an incorrect capture to occur.
Find What:
(?-s)Please(.+)?\R?E-mail((.+)?\R?){1,2}treatment
Replace With:<b>${0}</b>
So the OP can understand the regex:
(?-s)
= do not allow any.
to include the CR and LF (carriage return and line feed)
Please(.+)?\R?
= Find “Please” followed by possibly some characters excluding CR and LF, and then maybe a CR and LF
E-mail((.+)?\R?){1,2}
= Find “E-mail” followed by possibly some characters excluding the CR and LF and then possibly a CR and LF, allowing this set to occur once or twice. I haven’t fully tested but{2}
maybe OK as well.
treatment
= complete the selection with the word “treatment”
and in the Replacement field the${0}
refers to everything that has been found/selected.Of course if the OP’s files will ONLY ever include the word “Please” once there isn’t a reason for them to return to read this.
Terry
-
Hi, @terry-r,
Of course, in absolute terms, you’re quite right about it ! But I would say that people are responsable of what they write !
My regex finds a word
Please
, followed later with the nearest wordE-mail
, followed later with the nearest wordtreatment
If, because of some typos, one or several of these
3
words are truncated or modified in any way, due to the initial(?s)
modifier, the regex engine will always try to find out, by all means, a match, if any !Note that I suppose a possible variation by telling, in my previous post : " Check the
Mach case
option, if necessary "
Thus :
-
Any malformed word is just considered as stuff, part of the regex part
.+?
-
And for the smallest range of characters containing these
3
words in that order (Please ... E-mail .... treatment
) :-
A
<b>
string is inserted before the first wordPlease
-
A
</b>
string is inserted after the final wordtreatment
-
No more, no less !
BR
guy038
-