Easy task but driving me crazy



  • I was able to figure this out a few months ago from a thread somewhere deep in the web but can’t find the resource that helped me out before and have spent a long time trying to figure this out. My problem is laughably simple (I think).

    I have an xml file. I’ve created it so that in 16 different positions in the file, there is the number 9.3. I want to paste a column of 8 numbers (each different than 9.3), and create a macro (using probably find and replace, and running the macro 8 times), where each time I run the macro, it replaces the next two instances of 9.3 with the number at the top of the column, deleting the top row of the column as it does it. So after running the macro 8 times, each of the 8 numbers from the top column will appear in the program that the xml file describes (the original top number will be in the first two instances of 9.3, then the 2nd number in the 3rd and 4th instances, etc.). I cannot for the life of me figure out how to get this to work though! I have Notepad++ v7.5.6, and please let me know if I can provide any other info. (The way I described getting it to work is just how I got it to work the last time, but it was about 4 months ago and I can’t remember the details of how to rerun it.) Thanks in advance for all your help.



  • Easy is so relative. I thought this would be a simple macro but it seems that as soon as you start trying to paste things into the Find/Replace box (or the TextFX Quick Find box for that matter), everything starts to fall apart. Since NPP macros don’t memorize keystrokes, but feed back from Scintilla, I’m not sure that interacting with the dialog box is going to work. If I were you, I’d either work on a Python script to do what you’re trying to do, or you could find a keystroke recorder or Autoit or something. If you ever figure out how you did it last time, I’d be interested to hear.

    BTW, I think @guy038 had a regex way to handle something similar to this in another thread, but I can’t find it.





  • That is excellent! I was so hung up on using the Find/Replace I never considered just doing a find. The perl mantra TIMTOWTDI has never been truer. Thanks for following up and congrats on finding the lowest rated question on SO that I’ve found helpful!



  • Hello, @dan-mamerow, @cipher-1024 and All,

    As you said @cipher-1024, this task may, also, be achieved with regular expressions. The small drawback is that you’ll need to repeat the S/R as many times as the number of words in your list. But this task can just be done , repeatedly, until the message Replace: 0 occurrences were replaced occurs, and ends the task ;-))

    Let’s consider the following sample text, part of the license.txt file, where I inserted, at various locations, the string XYZ :

    The licenses for most software are designed
    to take away your freedom to share and change it.
    By contrast, the GNU XYZGeneral Public License is
    intended to guarantee your freedom to share and
    change free software--to make sure the software*
    is free for all its users. This General Public
    License applies to most of the Free Software
    Foundation's software and to any other programXYZ
    whose authors commit to using it. (Some other Free
    XYZSoftware Foundation software is covered by the GNU
    Library General Public License instead.) You can
    apply it to your programs, too.
    XYZ
    When we speak of free software, we are referring
    to freedom, not price. Our General Public Licenses
    XYZare designed to make sure XYZthat you have the freedom
    to distribute copies of free software (and charge
    for this service if you wish), that you receive source
    code or can get it if you want it, that you can change
    the software or use pieces of it in new fXYZree programs;
    and that you know you can do these things.
    XYZXYZ
    To protect your rights, we need to make restrictions
    that forbid anyone to XYZ deny you these rights or to ask
    you to surrender the rights. These restrictions translate
    to certain responsibilities for you if you distribute
    copies of the XYZ software, or if you modify it.
    XYZ
    
    • First, we count the number of occurrences of the string XYZ, with the simple regex (?-i)XYZ. We get 12 occurrences found. OK ! So, now, we add a list of 12 words / expressions, one per line, after our text, with a separator line ( I chose a ### line, but any string could be used ). So, our text becomes :
    The licenses for most software are designed
    to take away your freedom to share and change it.
    By contrast, the GNU XYZGeneral Public License is
    intended to guarantee your freedom to share and
    change free software--to make sure the software*
    is free for all its users. This General Public
    License applies to most of the Free Software
    Foundation's software and to any other programXYZ
    whose authors commit to using it. (Some other Free
    XYZSoftware Foundation software is covered by the GNU
    Library General Public License instead.) You can
    apply it to your programs, too.
    XYZ
    When we speak of free software, we are referring
    to freedom, not price. Our General Public Licenses
    XYZare designed to make sure XYZthat you have the freedom
    to distribute copies of free software (and charge
    for this service if you wish), that you receive source
    code or can get it if you want it, that you can change
    the software or use pieces of it in new fXYZree programs;
    and that you know you can do these things.
    XYZXYZ
    To protect your rights, we need to make restrictions
    that forbid anyone to XYZ deny you these rights or to ask
    you to surrender the rights. These restrictions translate
    to certain responsibilities for you if you distribute
    copies of the XYZ software, or if you modify it.
    XYZ
    ###
    Word1
    Word2
    Word3
    Word4
    Word5
    Word6
    Word7
    Word8
    Word9
    Word10
    Word 11
    This is the END
    

    Then :

    • Open the Replace dialog

    • Tick the Wrap around option

    • Select the Regular expression search mode

    Enter the regexes :

    SEARCH (?-i)XYZ(?s)(.+###\R)(?-s)(.+)\R?|###\R

    REPLACE ?2\2\1

    • Hold down the two keys ALT and A ( identical to a click on the Replace All button ) , till you see the message Replace: 0 occurrences were replaced, at the bottom of the Replace panel

    Voilà ! And we obtain the new text, below :

    The licenses for most software are designed
    to take away your freedom to share and change it.
    By contrast, the GNU Word1General Public License is
    intended to guarantee your freedom to share and
    change free software--to make sure the software*
    is free for all its users. This General Public
    License applies to most of the Free Software
    Foundation's software and to any other programWord2
    whose authors commit to using it. (Some other Free
    Word3Software Foundation software is covered by the GNU
    Library General Public License instead.) You can
    apply it to your programs, too.
    Word4
    When we speak of free software, we are referring
    to freedom, not price. Our General Public Licenses
    Word5are designed to make sure Word6that you have the freedom
    to distribute copies of free software (and charge
    for this service if you wish), that you receive source
    code or can get it if you want it, that you can change
    the software or use pieces of it in new fWord7ree programs;
    and that you know you can do these things.
    Word8Word9
    To protect your rights, we need to make restrictions
    that forbid anyone to Word10 deny you these rights or to ask
    you to surrender the rights. These restrictions translate
    to certain responsibilities for you if you distribute
    copies of the Word 11 software, or if you modify it.
    This is the END
    

    So, @dan-mamerow, in your case, let’s imagine the text, containing 8 occurrences of the decimal number 9.3 and the list of 8 numbers, below :

    9.3 The licenses for most software are designed
    to take away your freedom to share and change it.
    By contrast, the GNU9.3 General Public License is
    intended to guarantee your freedom to share and
    change free software--to make sure the software*
    is free for all its users. This General Public
    License applies to most of the Free Software
    Foundation's software and to any other program9.3
    whose authors commit to using it. (Some other Free
    9.3Software Foundation software is covered by the GNU
    Library General Public License instead.) You can
    apply it to your programs, too.
    9.3
    When we speak of free software, we are referring
    to freedom, not price. Our General Public Licenses
    9.3are designed to make sure 9.3that you have the freedom
    to distribute copies of free software (and charge
    for this service if you wish), that you receive source
    code or can get it if you want it, that you can change
    the software or use pieces of it in new f9.3ree programs;
    and that you know you can do these things.
    ###
    5.2
    5.2
    10.1
    10.1
    0.7
    0.7
    17
    17
    

    With the corresponding regex S/R :

    SEARCH (?-i)9\.3(?s)(.+###\R)(?-s)(.+)\R?|###\R

    REPLACE ?2\2\1

    You should see, after replacement, the expected text :

    5.2 The licenses for most software are designed
    to take away your freedom to share and change it.
    By contrast, the GNU5.2 General Public License is
    intended to guarantee your freedom to share and
    change free software--to make sure the software*
    is free for all its users. This General Public
    License applies to most of the Free Software
    Foundation's software and to any other program10.1
    whose authors commit to using it. (Some other Free
    10.1Software Foundation software is covered by the GNU
    Library General Public License instead.) You can
    apply it to your programs, too.
    0.7
    When we speak of free software, we are referring
    to freedom, not price. Our General Public Licenses
    0.7are designed to make sure 17that you have the freedom
    to distribute copies of free software (and charge
    for this service if you wish), that you receive source
    code or can get it if you want it, that you can change
    the software or use pieces of it in new f17ree programs;
    and that you know you can do these things.
    

    Nice, isn’t it ?

    Best Regards,

    guy038

    P.S. :

    BTW, @cipher-1024, what does this TIMTOWTDI acronym mean ? I did not see it, in this general list, below :-)

    https://en.wiktionary.org/wiki/Appendix:English_internet_slang#W



  • @guy038

    TIMTOWTDI = There Is More Than One Way To Do It

    :-D



  • And BTW I believe it is pronounced “Tim-Toady”

    :-)


Log in to reply