Replace differing text with one text
-
Hi
I have a lot of files where the text is like this
“abcdefg” that part is constant, then the next part could be anything and I want to replace the second part with a single text.
For example
“Tomorrow Situps”
“Tomorrow Stand”
“Tomorrow Run”I need to replace the text after Tomorrow with Kneel.
Is this possible
Thanks
savey
-
to replace an unknown word after a known word (constant) with a single text:
original text:
"Tomorrow Situps" "Tomorrow Stand" "Tomorrow Run"
notepad++ replace, search mode: regular expression
find what:"Tomorrow (.*?)"
replace with:
"Tomorrow Kneel"
then click on replace all
result:
"Tomorrow Kneel" "Tomorrow Kneel" "Tomorrow Kneel"
-
Hi
Thanks for your reply, I’m not sure what I am doing wrong,
My file content was Tomorrow Kneel
I put into find
Tomorrow (.*?)
I put into Replace with
Tomorrow Kneel
Pointed to the folder where the file was stored
selected Replace in FilesBut the content was not changed, I used a *.txt file type.
Also, I should have mentioned that the second word is sometimes
bracketed EG (Kneel)Hope this makes sense
Regards
savey -
Ooooppps!
I should have said
I put into Replace with
Tomorrow Runningsavey
-
@Savey-Traveller
yes, it is because your original example was within double quotes and the new example is not.regex search and replace always needs to know where to start and where to end
so if your example now is Tomorrow Situps instead of “Tomorrow Situps” you have to tell regex to search from the desired word till the end of the line:
Tomorrow (.*?)$(the $ tells regex to search until the end of the line)
if you use the above search with the quotes it won’t find the string because your new example does not have any quotes.
ps: maybe, just maybe it’s easier if you post the exact text for which you need the replace for, because fictional examples like yours often don’t reflect the task you need.
-
Hi
a very good idea, thanks for your help and patience.
The current data is like this, but there may bemore variables
and actually it is the data in the second group that needs to remain
and the first group changed, also enginedata and the spacing remains
unchanged and the brackets are essential. The actual lettering can be longer
or shorter but the spaces must be the sameEngineData ( gg1_4859 GG1_4859 )
EngineData ( hvycoach GG1_4859 )
EngineData ( hvycoach23a GG1_4859 )
the altered data needs to look like this
EngineData ( new_wagon GG1_4859 )
Hope this makes sense
thanks
savey
-
@Savey-Traveller
yes, this example and description makes perfect sense now
here are two regex replace examples you can play around withoriginal data:
EngineData ( gg1_4859 GG1_4859 ) EngineData ( hvycoach GG1_4859 ) EngineData ( hvycoach23a GG1_4859 ) EngineData ( gg1_4872 GG1_4872 )
search mode: regular expression
replace example 1, only replace the first group if GG1_4859 is present in the second group:
find what: \( (.*?) GG1_4859 \) replace with: \( new_wagon GG1_4859 \)
result:
EngineData ( new_wagon GG1_4859 ) EngineData ( new_wagon GG1_4859 ) EngineData ( new_wagon GG1_4859 ) EngineData ( gg1_4872 GG1_4872 )
replace example 2, this will search for any two data strings within the 2 brackets and only replace the first group, while keeping the second one in variable $2:
find what: \( (.*?) (.*?) \) replace with: \( new_wagon $2 \)
result:
EngineData ( new_wagon GG1_4859 ) EngineData ( new_wagon GG1_4859 ) EngineData ( new_wagon GG1_4859 ) EngineData ( new_wagon GG1_4872 )
note that if you make a regex search for a ( or ) bracket, you have to search for ( or ) using a leading backslash, because the actual characters ( ) tell regex that anything within them are a specific regex expression
-
Fantastic
thank you so much for your help.
Cheers
Daniel
-
Hello @savey-traveller and @meta-chuh,
I’m quite late but here is a more simple syntax, for your regex S/R :
SEARCH
\w+ +(?=GG1_4859)
, with a space between the two+
symbolsREPLACE
new_wagon\x20
Notes :
-
The first part
\w+ +
looks for a non-null range of consecutive word characters ( upper-case letter, lower-case letter, accentuated letter or digit ), followed by a **non-null range of consecutive space characters -
But, ONLY IF it’s followed by the exact string GG1_4859,
(?=GG1_4859)
-
If so, the word characters and space character(s), located before the string GG1_4859, are simply replaced by the exact string
new_wagon
, followed by a space character,\x20
So, given the original data :
EngineData ( gg1_4859 GG1_4859 ) EngineData ( gg1_1234 GG1_1234 ) EngineData ( hvycoach GG1_4859 ) EngineData ( hvycoach23a GG1_4859 ) EngineData ( gg1_4872 GG1_4872 )
you should obtain the text, below, where, only, the lines 1, 3 and 4 have been modified -:))
EngineData ( new_wagon GG1_4859 ) EngineData ( gg1_1234 GG1_1234 ) EngineData ( new_wagon GG1_4859 ) EngineData ( new_wagon GG1_4859 ) EngineData ( gg1_4872 GG1_4872 )
Best Regards,
guy038
-