Replace string
-
@Paolo-Serpi said in Replace string:
I’m looking for a method to replace automatically this tracknode string progression because it repeats 2399 times.
Hi Paolo
Did you mean something like this…?
This method requires the instalation of the Python plugin. If that is not an inconvenient, let me know it and I will provide you next steps.
Best Regards.
-
So this multiselect-via-Pythonscript-then-possibly-move-carets-via-BetterMulitiselection-then-insert-sequential-numbers-via-Column-Editor technique seems to be gaining some traction.
One thing to be cautious of for the last step: When inserting numbers via Column Editor, if the number text inserted changes width anytime during the sequence, the field will be space-padded for numbers that are less than the maximum width. This may or may not cause a problem, depending upon one’s application.
As an example, if you have 5 carets and you use the Column Editor to insert 8 through 12, what you’ll actually get is 8space, 9space, 10, 11, and 12. Obviously this doesn’t apply if you tick the Leading Zeroes checkbox–in that case you surely should know in advance what you’ll get–but it could be surprising and undesirable in some situations.
-
@astrosofista Yes! This is exactly what i’m lookin’ for! So i’ve installed Phyton plugin can you explain how you can do the multiselection?!
Thanks
-
The Pythonscript shown here would need to be run after you’ve made a selection out of one section of your common text; like @astrosofista shows above with the
TrackNode
text. Reading other posts in the same thread will fill in any remaining gaps in the technique, I think. -
@Paolo-Serpi said in Replace string:
@astrosofista Yes! This is exactly what i’m lookin’ for! So i’ve installed Phyton plugin can you explain how you can do the multiselection?!
Thanks
Hi Paolo:
Have you installed the Python script (PS) mentioned and linked by @Alan-Kilborn? If you did so, check
Preferences > Editing > Multi-Editing Settings > Enable (Ctrl+Mouse click/selection)
if needed. Then copy and paste the text to be changed in a new tab, so we are sure that replacements don’t apply in unwanted zones.- Now, select the first
TrackNode (
up to the number —1
in my gif animation—. - Run
PS
in order to select all the instances to change. - Press the
Right Arrow
only one time. All the carets are located at the left of the numbers to be replaced. - Press
Shift + End
to select the numbers. - Open
Column Editor
and enter the initial number of the series, as it is showed in my gif.
Once you are done, press
Escape
get ride of all those carets. - Now, select the first
-
Hello, @paolo-serpi, @astro-sofista, @alan-kilborn and All,
Here is an alternative solution to your problem, which uses regular expressions !
Assuming this small input text, below :
TrackNodes (2399 TrackNode ( 1 TrVectorNode ( blab blah blab blah ) TrPins ( 1 1 blah blah blah blah ) ) TrackNode ( 2 TrVectorNode ( blab blah blab blah ) TrPins ( 1 1 blah blah blah blah ) ) TrackNode ( 3 TrVectorNode ( blab blah blab blah ) TrPins ( 1 1 blah blah blah blah ) )
-
Put the caret at beginning of line
TrackNodes (2399
-
Open the Replace dialog (
Ctrl + H
) -
SEARCH
(?-is)(TrackNode\x20\(\x20)\d+|\R(?!.*?TrackNode)
-
REPLACE
?1\1><:|
-
Select the
Regular expression
search mode -
Click on the
Replace All
button
Immediately, we get the text :
TrackNodes (2399 TrackNode ( ><| TrVectorNode (| blab blah| blab blah| )| TrPins ( 1 1| blah blah| blah blah| )| ) TrackNode ( ><| TrVectorNode (| blab blah| blab blah| )| TrPins ( 1 1| blah blah| blah blah| )| ) TrackNode ( ><| TrVectorNode (| blab blah| blab blah| )| TrPins ( 1 1| blah blah| blah blah| )| )|
Remark : I assume that the Vertical bar char (
|
), which replaces any line-break, does not exist, yet, in your file. If it’s not the case your may use, in the replace field, any other single standard char, not present in your file !Now, it’s almost obvious :
-
Put the caret on the first line
TrackNode
, between the two symbols>
and<
-
Open the column editor (
Alt + C
) -
Select the choice
Number to insert
-
Type in the number
8103
as initial number -
Type in the value
1
, as increment -
Type in the value
1
, as Repeat sequence -
Do not tick the
Leading zeros
box -
Click on the
OK
button -
If necessary, delete the very last line, with the number only !
You should get :
TrackNodes (2399 TrackNode ( >8103<| TrVectorNode (| blab blah| blab blah| )| TrPins ( 1 1| blah blah| blah blah| )| ) TrackNode ( >8104<| TrVectorNode (| blab blah| blab blah| )| TrPins ( 1 1| blah blah| blah blah| )| ) TrackNode ( >8105<| TrVectorNode (| blab blah| blab blah| )| TrPins ( 1 1| blah blah| blah blah| )| )|
- Finally, perform the following regex S/R :
SEARCH
[<>]|(\|)
REPLACE
?1\r\n
( or?1\n
if you work with UNIX files ! )And…, here is your expected text :
TrackNodes (2399 TrackNode ( 8103 TrVectorNode ( blab blah blab blah ) TrPins ( 1 1 blah blah blah blah ) ) TrackNode ( 8104 TrVectorNode ( blab blah blab blah ) TrPins ( 1 1 blah blah blah blah ) ) TrackNode ( 8105 TrVectorNode ( blab blah blab blah ) TrPins ( 1 1 blah blah blah blah ) )
If you like it, I’ll explain the regexes’s syntax, next time ;-))
Best Regards,
guy038
-
-
In this particular case the
BetterMultiSelection
plugin is not needed, Just Notepad++ standard multiselection feature is the one that moves the carets. Know that because I disabled the plugin when checking the method.But yes, I also noticed those blank spaces and they could be problematic, as you said. If needed, a S/R regex can easily remove them. In Paolo’s case would be
(?-s)TrackNode \( \d+\K\x20+
, and leaving empty theReplace
field.Best Regards.
-
@astrosofista I installed the PS script following the discussion linked by @Alan-Kilborn, but i can’t run PS like you said at point 2.
Can you explain better how i can do it?I try the method of @guy038 and works perfectly.
-
@Paolo-Serpi said in Replace string:
i can’t run PS like you said at point 2.
Can you explain better how i can do it?OK, once the Python script plugin has been installed and grabbed the Python code from the linked page:
- Go to
Plugins > Python Script > New Script
. - Give it a name, say “Select all the same words”, and press save.
- A new empty editor should appear. Copy the content into it and save it.
Please take care to paste the code as it is, avoiding any change, as Python is strict about whitespaces.
- Go back to the
TackNode
page. - Select the first
TrackNode (
, including the blank space before the number. - Run the Python script. To do so, go to
Plugins > Python Script > Scripts
, andselect the PS as you named
—“Select all the same words” in my example—. - All the
TrackNode
instances should be selected. - Continue with the other steps described in the previous post.
Let me know what happened.
- Go to
-
@Paolo-Serpi
In addition to what @astrosofista wrote,-
use the console (plugins->python script->show console) in case something doesn’t work as expected to see if an error is logged
-
by using the plugins->python script->configuration dialog you can add
your script to menu items. Once this is done you can assign a shortcut
via run->modify shortcut/delete command dialog. Goto plugins tab,
search for the script and assign a shortcut.
-
-
@astrosofista Well, i try a lot of times but never happen when i run PS following your suggestions. Notepad++ select only the first "tracknode ( ". I don’t know why but the only method that up to now work is the @guy038 ‘s method.
I hope that this method will be useful in similar situations, because i’m lookin’ for a standard method that i can always use in these cases. -
@Paolo-Serpi said:
select only the first "tracknode
Did you pay attention to what @astrosofista had said:
check Preferences > Editing > Multi-Editing Settings > Enable (Ctrl+Mouse click/selection)
You need to tick that
Enable
box.
This might be your problem.Note: as an alternative, it could be scripted as follows:
editor.setMultipleSelection(True) # in case not enabled in the Preferences
-
I see. Let’s make sure that the
Python plugin
is correctly installed. I suppose you installed it with thePlugins > Plugins Admin...
menu.Now we are going to see if it is working properly. Please click
Plugins > Python Script > Show Console
. On the bottom of the console there is a text box, please type in the following codenotepad.new()
and press
Run
. A new document should be opened. Got it?If it worked, check again
Preferences > Editing > Multi-Editing Settings > Enable (Ctrl+Mouse click/selection)
.Try once more time to run the Python script, as described in previous posts.
Other than these ones, I am running out of ideas.