multiple text replacing with different text
-
Your request wasn’t abundantly clear, but I have a guess as to what you meant. (Using Random as the example is probably not the best choice, because we’re left to wonder whether every instance of the search-match will be the same text, or whether it will be different in each location.)
It might be possible using regexes, if you are willing to add the list of replacement words to the same document in some uniquely identifiable format. There have been examples of doing that in the past in this forum, and it wouldn’t surprise me if one of our regex guru dug one of those solutions out (or re-developed it on the fly).
However, if I were to solve a problem similar to this, I would probably fire up my PythonScript plugin and write a python-based script using
editor.rereplace()
to match the Random text and use a callback function to replace it with the next item from the list.However, before any of us spend time developing a solution, it would be good of you to confirm my interpretation, and provide a couple of clarifications.
I assume you have data that looks like the text below. (I am using
KEYWORD
instead of Random to avoid the confusion; this means that I am assuming the text will be literallyKEYWORD
at any point in the document while you’re doing the search/replace.)this is some text with KEYWORD multiple times, might have KEYWORD twice on a line, or a line might have it never KEYWORD blah KEYWORD done
and then there is a list of words, like
ONE TWO THREE FOUR
that you want those replaced with:this is some text with ONE multiple times, might have TWO twice on a line, or a line might have it never THREE blah FOUR done
I don’t pretend that it’s really your data. Please confirm my interpretation: is that similar to the situation you have? If not, please give a better example of before/after data, so that we can better understand what you really have. (See the advice at the bottom for how to format the post like I did. Also, notice how my example had examples of text that changed, and examples of text that didn’t change. Edge cases and text that doesn’t change are as critical to such questions as the data that you do want changed.)
And for clarifications:
- What do you want to happen if you have
KEYWORD
four times, but only have two replacement words? (for example,blah KEYWORD blah KEYWORD blah KEYWORD blah KEYWORD
withONE TWO
)- should it just not do any replacements, and give an error?
- should it just replace the first two
KEYWORD
instances:blah ONE blah TWO blah KEYWORD blah KEYWORD
- should it wrap around and start from the beginning of your replacement list again?
blah ONE blah TWO blah ONE blah TWO
- something else I haven’t guessed
- Similarly, what should happen if the replacement list is longer than the number of keywords? (for example,
blah KEYWORD blah KEYWORD blah
withONE TWO THREE FOUR
)- my best guess is
blah ONE blah TWO blah
- is it something different than I guessed?
- my best guess is
----
Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the
</>
toolbar button or manual Markdown syntax. To makeregex in red
(and so they keep their special characters like *), use backticks, like`^.*?blah.*?\z`
. Screenshots can be pasted from the clipboard to your post usingCtrl+V
to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get. Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries. - What do you want to happen if you have
-
Hi @PeterJones
Thank you and sorry for the confusion
you explained my idea exactly for how I want it to be
that was the same as what I meant,
I will give a more example with the word USA instead of using the word KEYWORD this time.1- have you ever visited the USA 2- the USA Freedom Act. 3- she lives in the USA
I want to replace the word “USA”
To: my list contains different whole other countries names “by order”
1- CANADA 2- MEXICO 3- COSTA RICA- have you ever visited the CANADA
- the MEXICO Freedom Act.
- she lives in the COSTA RICA
same sentences just the “USA” changes
let’s say in the list of words that I want to change to
has more countries names more than what word “USA” is included in the text
I guess it’s the logic to just change where the word “USA” is included or
personally I would Accept whatever just to fix my current problem and save time -
If my list has an insufficient number of words than of how many times the word “USA” included the best would be not to repeat the words from 1- 2- again from the start and just finish replacing
-
regex solution
This depends on how long your main data is; if it’s too long, the regex capture groups might run out of memory. But it might be worth trying.
For my “special formatting” for your replacement list, I am going to use a line with
~~~~~~
, then∙
followed by each replacement string. (I picked∙
because it’s not likely to be in your existing data; you can choose a different separator if you like; just make sure it’s not found elsewhere, and that you change it throughout these instructions- Add the special formatting with the list at the end of your data:
1- have you ever visited the USA 2- the USA Freedom Act. 3- she lives in the USA ~~~~~~ ∙CANADA∙MEXICO∙COSTA RICA
- Hit
Ctrl+HOME
or equivalent to go to the first character in the file. - Search > Replace
- MODE = regular expression
- ☑ WRAP AROUND = make sure it’s checked on
- FIND =
(?s)USA(.*?~+\s*)∙([^∙\r\n]*)
- REPLACE =
${2}${1}
- click FIND NEXT once, then hit REPLACE once for each replacement word (so three times for the three countries in the example). (REPLACE ALL will just replace the first instance because it’s capturing to nearly the end in order to delete each replace-text once it’s used)
- Manually delete the
~~~~~~
line from the end
Python Script
This requires installing the PythonScript plugin. (You can do that using Plugins Admin)
- Plugins > Python Script > New Script: saving as
replaceList.py
- Use the following as the text of the document, and SAVE
# encoding=utf-8 """in response to https://community.notepad-plus-plus.org/topic/21225/ Written by @PeterJones Searches for keywordText multiple times, and replaces it with the next element of replacementList """ from Npp import * keywordText = 'USA' replacementList = [ 'CANADA', 'MEXICO', 'COSTA RICA' ] counter = -1 def nextReplacement(m): global counter counter += 1 return replacementList[counter] if counter < len(replacementList) else m.string editor.beginUndoAction() editor.replace(keywordText, nextReplacement) editor.endUndoAction()
- Change the
keywordText
andreplacementList
variables to match your desired KEYWORD and list of replacements, using the same syntax as shown in the example. SAVE - Open (or switch to) your file.
- Plugins > Python Script > Scripts >
replaceList
- At this point, the replacement should be done. If you need to, a single UNDO should work to undo all the replacements
That sequence works for me on your example data
caveat emptor
This script seemed to work for me, based on my understanding of your issue, and is published here to help you learn how to do this. I make no guarantees or warranties as to the functionality for you. You are responsible to save and backup all data before and after running this script. If you want to use it long term, I recommend investing time in adding error checking and verifying with edge cases.
- Add the special formatting with the list at the end of your data:
-
@PeterJones said in multiple text replacing with different text:
Python Script
Interesting; when I tried the script I got this error:
return replacementList[counter] if counter < len(replacementList) else m.string <type 'exceptions.RuntimeError'>: This method or property is not supported under Notepad++ PythonScript
I would guess this is related to usage of
m.string
which I have never seen before. Maybe this is only supported in a P.S. that is newer than the one I use: 1.5.4Anyway, I tried out the script because I was going to suggest the following alternative method, and it just so happens that it avoids that error for me as well.
Change the “return” line to:
return replacementList[counter]
and change the “.replace” line to:
editor.replace(keywordText, nextReplacement, 0, 0, editor.getLength(), len(replacementList))
This is instructive because it shows how to use PythonScript code to limit the number of replacements made; in this case, to the length of the replacement list.
-
I am using PythonScript 1.5.4, so that’s not it
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] Initialisation took 750ms Ready.
Oh, wait, my example didn’t try to go beyond, so was never triggering the ELSE. when I add some more USA to the example data, then it does give me the error.
I confirmed in the PythonScript 1.5.4 docs that its MatchObject doesn’t implement the re.MatchObject’s
.string()
method.
However, since the match string will always be identical to
keywordText
(because it’s a text search, not a regex search), if I change that line to
return replacementList[counter] if counter < len(replacementList) else keywordText
… then the error goes away and everything works even with too manyUSA
instances.But yes, I like your idea of using the optional parameters to avoid having to do the conditional return.
-
ah, also, instead of
m.string()
, I can usem.group(0)
– group#0 is the whole match, even when it’s not a grouped or regex-based replacement. -
@PeterJones said in multiple text replacing with different text:
1- have you ever visited the USA
2- the USA Freedom Act.
3- she lives in the USAthanks for the help @PeterJones much much appreciated
I have tried installing the python code without changing any of the text when I tried on this example:1- have you ever visited the USA 2- the USA Freedom Act. 3- she lives in the USA
it does not change anything
-
@Handa-Flocka said in multiple text replacing with different text:
it does not change anything
Can you tell me the exact steps you took, because it works for me.
-
… for example, here’s a screen-capture video:
-
Now it works like a charm!! I forgot some steps during the process
both of them now are working perfectly
it would be preferred to use python script instead of regex in this process if I am dealing with huge data?Thank you for your help so much appreciation man
-
@Handa-Flocka said in multiple text replacing with different text:
it would be preferred to use python script instead of regex in this process if I am dealing with huge data?
Correct. Large amounts of data will mean that the
(.*?~+\s*)
capture groups grows beyond the regex capture-group memory allocated in Notepad++. -
got it!
Thank you and for the effort you put on this -
I am replying to your Chat message here. Questions like this belong in the Forum posts, not in the chat.
Sorry If bothering you
I have tried again to run the same py script on some random code it didn’t want to change
codeEVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
for example I am selecting word
TYPEE
it doesn’t change at allMy guess is that the chat window got rid of the <open> tags on most of those. But the example included
TYPEE
, and that’s likely all that’s critical for the replacement.- Change
replaceList.py
sokeywordText = 'TYPEE'
; SAVE - Open file with example text
EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
- Plugins > PythonScript > Scripts >
replaceList
- Result
… SoEVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“CANADA” MODIFIERS=shift
TYPEE
has becomeCANADA
So it works for me.
- Change
-
@PeterJones said in multiple text replacing with different text:
EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
@PeterJones my bad I thought the thread was locked!
the code works perfectly in one line
but If I tried something like this for exampleEVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
then it will not work!
-
@Handa-Flocka said in multiple text replacing with different text:
then it will not work!
Sorry, I don’t know what you’re doing differently. With no change compared to my sequence above (that is, with
TYPEE
as the keyword and the same list of three countries as the replacementList), I get:EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“CANADA” MODIFIERS=shift EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“MEXICO” MODIFIERS=shift EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“COSTA RICA” MODIFIERS=shift EVENTS TYPE=keypress SELECTOR=“body>div.page_wrap>div.im_page_wrap.clearfix>div>div.im_history_col_wrap.noselect.im_history_loaded>div.im_history_selected_wrap>div>div.im_bottom_panel_wrap>div.im_send_panel_wrap.noselect>div>div>div>form>div.im_send_field_wrap.hasselect>div.composer_rich_textarea” CHARS=“TYPEE” MODIFIERS=shift
Since my list only had 3 countries, the fourth
TYPEE
remains unchanged.Here is my exact
replaceList.py
with your data:# encoding=utf-8 """in response to https://community.notepad-plus-plus.org/topic/21225/ Written by @PeterJones Searches for the value of keywordText multiple times, and replaces it with the next element of replacementList """ from Npp import * keywordText = 'TYPEE' replacementList = [ 'CANADA', 'MEXICO', 'COSTA RICA' ] counter = -1 def nextReplacement(m): global counter counter += 1 return replacementList[counter] if counter < len(replacementList) else m.group(0) editor.beginUndoAction() editor.replace(keywordText, nextReplacement) editor.endUndoAction()
You might want to go to Plugins > PythonScript > Show Console and look for errors. (You can right click in the console window, click Clear, then run the script again on your data file. If anything is printed in the console, it’s an error message.) The error message will likely indicate where you have a typo or syntax error in the script that was introduced when you put in your new
keywordText
andreplacementList
. If you need help with that, copy/paste the errors you see into the forum (as well as your modified copy ofreplaceList.py
) -
This post is deleted! -
This post is deleted! -
@PeterJones After your update it’s a piece of art now!
thanks again