Find and replace in xml file,



  • @PeterJones

    Yes, sorry, I meant the example from replace(), not rereplace() although the regex version of the replace has a nice example too.

    @Guillaume-M-CHAZERANS

    So given your example, best I can tell, the following script could do such a replacement you described:

    counter = 0
    
    the_list = [
        'iteration1',
        'other text',
        '3rd text chain',
    ]
    
    def get_counter(m):
        global counter
        ret = the_list[counter]
        counter += 1
        if counter >= len(the_list): counter = 0
        return ret
    
    editor.replace('text0', get_counter)


  • Wow, I submitted my script, and it was right below Chagui’s last post, and I was looking it over to see if I needed to edit it (within 3 mins!) when, BLAMMO!, in pops Peter’s posting including a script, right in between! I thought I was going crazy. How does this happen?



  • Apparently, your browser didn’t do the behind-the-scenes auto-refresh. When I’m writing a response and someone else posts a reply before I’m done, it will usually load/update behind the editor; sometimes, I see the flash, or the bell highlight or the unread-highlight before I post. But I don’t know if it always happens.

    With a 20min difference in post-time (14:37Z vs 14:57Z), I am surprised mine hadn’t shown up before you posted.



  • @Alan-Kilborn

    the first user that starts writing in nodebb will be above another reply, if another user has started to write later, even if this second user submitted his post before.

    you might have noticed it already, if you look at the time stamp (about x minutes ago) of threads with many replies at the same time, you’ll sometimes see, that a newer post is ordered above an older one, instead of below.

    note: what i usually do, if it’s not a chat but a thread to be answered, is to scroll up and have a look if someone is already writing an answer.
    if someone is writing, i don’t start typing at all, and wait until he/she finished, to avoid having two similar answers.



  • @Meta-Chuh

    Two similar answers are not a bad thing. I enjoyed reading Peter’s script solution to see how it was similar to and different from mine. :)



  • Hi, @alan-kilborn, @peterjones and All,

    After trying your both scripts, here is my own version, using the modulo method of Peter, in the Alan script :

    the_list = [
        'iteration1',
        'other text',
        '3rd text chain',
    ]
    
    l = len(the_list)
    
    counter = l - 1
    
    def get_counter(m):
        global counter
        counter = ( counter + 1 ) % l
        return the_list[counter]
    
    editor.replace('text0', get_counter)
    

    I don’t know if getting the length of the list, in the variable l, is faster than calculating it, each time there is a match of the ‘text0’ string ?


    Now, Alan, I’m remembering of your nice script, some days ago, that I’ve slightly modified :

    https://notepad-plus-plus.org/community/topic/16942/pythonscript-any-ready-pyscript-to-replace-one-huge-set-of-regex-phrases-with-others/23

    And I was wondering if we could merge these two scripts in a single script, using a SR_List.txt file, like below :

    # ----- File SR_LIST.txt -----
    
    # Change 1st occurrence of 'text0' with 'ABC'
    # Change 2nd occurrence of 'text0' with ' DEF '
    # Change 3rd occurrence of 'text0' with '<GHI>'
    # Change 4th occurrence pf 'text0' with 'ABC', and so on... :
    
    !text0!ABC! DEF !<GHI>!
    
    # STANDARD case  : change ANY occurrence of 'text1' with the sentence 'This is a test' :
    
    %text1%This is a test%
    
    # Change 1st occurrence of 'text2' with '(012)'
    # Change 2nd occurrence of 'text2' with '[345]'
    # Change 3rd occurrence of 'text2' with '{678}'
    # Change 4th occurrence pf 'text2' with ' 901 '
    # Change 5th occurrence of 'text2' with '(012)', and so on... :
    
    =text2=\(012\)=[345]={678}= 901 =
    
    # Change 1st occurrence of 'text3' with 'Bravo !!'
    # Change 2nd occurrence of 'text3' with 'Yeah!!'
    # Change 3rd occurrence of 'text3' with '{Bravo!!', and so on... :
    
    @text3@ Bravo!!@Yeah!!@
    

    Just an idea, of course ! Above all, do not feel obliged to create such a script ;-))

    Cheers,

    guy038



  • Hi all,

    Ha, ha ! So, friends, you didn’t notice my mistake : my present script works, only, with a 1 or 3 items list :-((

    Of course, the initialization of the counter variable must be : counter = l - 1 ( and not counter = 2 )

    I’ve updated my previous post, as well !

    BR

    guy038



  • @guy038

    the modulo method of Peter

    I often do the modulo method, for myself, but I thought the >= len() compare clearer for the noobs.

    if getting the length of the list, in the variable l, is faster than calculating it, each time

    I’m sure it is, marginally…or not so marginally if we are talking “big data”.

    BTW, I was trying to keep my script maximally “in-flavor” with the Pythonscript docs editor.replace() example, since we cited that earlier.

    merge these two scripts in a single script

    Surely. Go for it! :)



  • @guy038

    Ha, ha ! So, friends, you didn’t notice my mistake …

    until now, there was no need to test everything you write, as you have a guru status, and you’re known to be very, very thorough at testing everything before posting … but from now on … 😂😂😂

    just kidding, your mistakes are unnoticeably few, and rest assured, i’m producing far more mistakkes and thypos every month 😉👍



  • He guys

    I didn’t know only python gurus were working on this issue. ;) Thanks a lots for all these examples!
    I check them (beginning by the last one?) to resolve my issue

    chagui


Log in to reply