Replacing number with another Incrementing #
-
Now THAT is a serious workaround, for the truly desperate. :-)
-
@Alan-Kilborn said in Replacing number with another Incrementing #:
Now THAT is a serious workaround, for the truly desperate. :-)
Yep, looks as a quite hard task, but actually isn’t. It would be easier to deliver if the macro feature could record Column Editor outputs.
Later will try your nice Python script.
-
@Alan-Kilborn said in Replacing number with another Incrementing #:
Specify ^\d{3}(?=#) in the input box that appears. Press OK.
Tested and worked fine on sample text :)
However, I found a potential failure. OP told us that he wanted to replace 30,000 numbers, so if these grow by one, as the sample text suggests, then the regex you provided will fail to match four or five digit numbers.
If this is the case, then as you know, expressions like
^\d{3,5}(?=#)
or^\d+(?=#)
will match all the numbers. -
I found a potential failure.
True, but also kind of obvious. :-)
Plus, I should have used “e.g.” on that part of it, like I did in 2 other places.But yes, the OP may not be versed in regex, and may not know how to specify a solution that covers all his cases, so thanks for the pick-me-up on that.
For me, it was more about publishing the script, which I had sitting unfinished in my N++ tabs ever since @Ekopalypse published his original script (which only selected multiple occurrences of static text).
-
@Alan-Kilborn said in Replacing number with another Incrementing #:
For me, it was more about publishing the script
Rest assured your script is a nice and useful improvement, since it allows users to make more complex selections with greater flexibility.
Thank you for sharing it with us :)
-
Hi @raizkie19, @Ekopalypse, @Alan-Kilborn, All
I have just realised that, given the regularity of the problem in question, it can also be stated as a mathematical operation, a simple addition. This approach, which perhaps was the one that @ekopalypse had in mind in his post above, gives rise to a third alternative, simpler and more direct than the two posted so far, since although it still requires the Python plugin and a regex, it doesn’t need any list created with the Column Editor.
The following script is a very minor adaptation of a code posted by @ekopalypse to solve a similar problem, so all credits belongs to him:
from Npp import editor def add_raizkie_number(m): return int(m.group(0)) + 39901 editor.rereplace('\d+(?=#)', add_raizkie_number)
So, @raizkie19 hope you can test it and see if it deliver the expected outcome. It worked fin here.
Have fun!
-
@astrosofista said in Replacing number with another Incrementing #:
very minor adaptation of a code posted by @ekopalypse to solve a similar problem
so all credits belongs to himActually, I think all of the credit goes back to the Pythonscript documentation!
Note, though:
number
should beint
in the documentation! -
More specifically, I have modified the result of calling help(editor.rereplace) in the PythonScript console.
>>> help(editor.rereplace) Help on method rereplace: rereplace(...) method of Npp.Editor instance rereplace( (Editor)arg1, (object)searchRegex, (object)replace) -> None : Regular expression search and replace. Replaces 'searchRegex' with 'replace'. ^ and $ by default match the starts and end of the document. Use additional flags (re.MULTILINE) to treat ^ and $ per line. The 'replace' parameter can be a python function, that recieves an object similar to a re.Match object. So you can have a function like def myIncrement(m): return int(m.group(1)) + 1 And call rereplace('([0-9]+)', myIncrement) and it will increment all the integers.
-
@Alan-Kilborn said in Replacing number with another Incrementing #:
Note, though: number should be int in the documentation!
That documentation bug was fixed in v1.5.3 in February.
v1.5.4 has been released since, with the fix to the getLanguageDesc() historical bug which we finally reported in #146. -
May it be, mine was just a disclaimer, as I know almost nothing about Python :)