Find and replace a variable item



  • Hi guys, first time posting!

    I’m working on a .xml file and need to replace every item that says:

    lootmax=“6”

    The issue I’m having, is the 6 is variable, there’s over 500 lines that have ranging numbers but I want to set them all to one number.

    Hope this is clear enough, many thanks.



  • If even possible, I’d like to up all the numbers incrementally from their original values, for example the file may have the following:

    lootmax=“2”
    lootmax=“4”
    lootmax=“6”
    lootmax=“1”
    lootmax=“5”
    lootmax=“9”

    Would I be able to find all values called lootmax="#" and up that incrementally by 1? So they would all now be

    lootmax=“3”
    lootmax=“5”
    lootmax=“7”
    lootmax=“2”
    lootmax=“6”
    lootmax=“10”



  • Replacing with a fixed number can be solved with a regular expression
    like this find what: (?!lootmax=")\d
    This would look for the text lootmax=" followed by ONE digit
    In replace with you put in what you like to have.
    If it might be possible that there are two digit numbers or even greater numbers
    you have to provide an additional multiplier + -> \d+

    Doing math with regular expression is, in my opinion, choosing the
    wrong tool for the job. You can workaround it by doing mapping things
    like if you find 2 replace by 3, if 3 then replace with 4 …
    but that means that you have to create the mapping list yourself.
    I think for such cases a programming language might be the better choice.
    There are a plenty of plugins available, like pythonscript, luascript … which allow you to manipulate the text from a written script.
    Let us know what you want to do.



  • So, what you are asking for is not doable in a simple search-and-replace, even with regex. There are tricks you could play (and that @guy038 might chime in with) to simulate adding one, since you always want to do that, but search-and-replace/regex are not really designed for arbitrary math.

    The scripting plugins – like PythonScript or LuaScript – allow you to bring to bear the full power of a programming language onto the contents of the files opened inside Notepad++. The docs that bundle with PythonScript even give an example of “add 1 to matched number”:

    def add_1(m):
        return 'Y' + str(number(m.group(1)) + 1)
    
    # replace X followed by numbers by an incremented number
    # e.g.   X56 X39 X999
    #          becomes
    #        Y57 Y40 Y1000
    
    editor.rereplace('X([0-9]+)', add_1);
    

    It’s a couple of tweaks to make it work for you: Assuming literal ASCII quotes, rather than the smartquotes that show up in your post, then the regex could be something like:

    def add_1(m):
        return (m.group(1)) + str(int(m.group(2)) + 1) + (m.group(3))
    
    editor.rereplace('(lootmax=")([0-9]+)(")', add_1);
    

    The regex puts lootmax=" into group(1), the number in group(2), and the end-quote in group(3). The function replaces that with group(1), then the value-of-group(2)-plus-1, then group(3). The editor.rereplace() call runs that regex-and-function on the whole active document.

    (I see that @Ekopalypse chimed in suggesting PythonScript and LuaScript while I was writing this up.)



  • Question for Python experts: I noticed that @bruderstein’s example add_1() definition in the PythonScript manual included the number() function, which doesn’t seem to exist in either Python 2 or Python 3.

    Traceback (most recent call last):
      File "C:\usr\local\apps\notepad++\plugins\Config\PythonScript\scripts\18860-add1.py", line 4, in <module>
        editor.rereplace('lootmax="([0-9]+)"', add_1);
      File "C:\usr\local\apps\notepad++\plugins\Config\PythonScript\scripts\18860-add1.py", line 2, in add_1
        return 'Y' + str(number(m.group(1)) + 1)
    NameError: global name 'number' is not defined
    

    Was that an old/deprecated function? Or something in another library? Or just a mistake? I made mine work with int(), but I was just curious whether that’s something that should be raised as an issue.



  • @PeterJones, you are right, int is the right function.
    I’m unaware whether there was a number function
    in some pre 2.7 versions but since 2.7 there is nothing listed.



  • issue#141 created. PR#142 created.



  • @PeterJones thank you for your reply, and to others also contributing.

    Peter, is it possible to send you a private message of the .XML file I have? My knowledge in doing such a task is, minimal to say the least.



  • @PeterJones said in Find and replace a variable item:

    issue#141 created. PR#142 created.

    I forgot to go back and look. This has been fixed in v1.5.3.



  • @Tazhar0001 said in Find and replace a variable item:

    Peter, is it possible to send you a private message of the .XML file I have? My knowledge in doing such a task is, minimal to say the least.

    There are no PM on this, and I don’t advertise my email address here (avoiding spambot harvesting).

    The [example data](https://community.notepad-plus-plus.org/post/50274 you showed near the start of this thread does work with the script I provided (well, I guess my previous post was missing the first line needed):

    from Npp import *
    
    def add_1(m):
        return (m.group(1)) + str(int(m.group(2)) + 1) + (m.group(3))
    
    editor.rereplace('(lootmax=")([0-9]+)(")', add_1);
    

    Even with data like:

    lootmax="2"
    lootmax="4"
    lootmax="6"
    lootmax="1"
    lootmax="5"
    lootmax="9"
    
    <tag other="x" lootmax="2" />
    <tag other="y" lootmax="4" />
    <tag other="z" lootmax="6" /> <other>blah</interferes>
    <tag other="p" lootmax="1" />
    <tag other="d" lootmax="5" />
    <tag other="z" lootmax="9" />
    

    all the lootmax="#" get one added properly.

    If you need help with PythonScript

    1. Install PythonScript (if you don’t already have it): Plugins > Plugins Admin, check the box on ☐ PythonScript, and click Install; restart Notepad++ as needed
    2. Plugins > PythonScript > New script, named lootmax.py
    3. Paste the contents of the script from this post. Save.
    4. Set Notepad++'s active file to your file with the lootmax values. Run **Plugins > Python Script > Scripts > lootmax`; the replacement should occur.

Log in to reply