• Login
Community
  • Login

Find and replace in xml file,

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
17 Posts 5 Posters 3.8k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A
    Alan Kilborn @PeterJones
    last edited by Jan 31, 2019, 2:57 PM

    @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)
    
    1 Reply Last reply Reply Quote 3
    • A
      Alan Kilborn
      last edited by Alan Kilborn Jan 31, 2019, 3:00 PM Jan 31, 2019, 3:00 PM

      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?

      M 1 Reply Last reply Jan 31, 2019, 5:29 PM Reply Quote 0
      • P
        PeterJones
        last edited by Jan 31, 2019, 5:00 PM

        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.

        1 Reply Last reply Reply Quote 1
        • M
          Meta Chuh moderator @Alan Kilborn
          last edited by Meta Chuh Jan 31, 2019, 5:30 PM Jan 31, 2019, 5:29 PM

          @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.

          A 1 Reply Last reply Jan 31, 2019, 6:12 PM Reply Quote 1
          • A
            Alan Kilborn @Meta Chuh
            last edited by Jan 31, 2019, 6:12 PM

            @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. :)

            1 Reply Last reply Reply Quote 1
            • G
              guy038
              last edited by guy038 Jan 31, 2019, 7:48 PM Jan 31, 2019, 6:30 PM

              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

              1 Reply Last reply Reply Quote 3
              • G
                guy038
                last edited by guy038 Jan 31, 2019, 7:49 PM Jan 31, 2019, 7:47 PM

                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

                A M 2 Replies Last reply Jan 31, 2019, 8:01 PM Reply Quote 2
                • A
                  Alan Kilborn @guy038
                  last edited by Alan Kilborn Jan 31, 2019, 8:01 PM Jan 31, 2019, 8:01 PM

                  @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! :)

                  1 Reply Last reply Reply Quote 1
                  • M
                    Meta Chuh moderator @guy038
                    last edited by Meta Chuh Jan 31, 2019, 8:31 PM Jan 31, 2019, 8:12 PM

                    @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 😉👍

                    1 Reply Last reply Reply Quote 2
                    • G
                      Guillaume M. CHAZERANS
                      last edited by Feb 1, 2019, 2:52 PM

                      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

                      1 Reply Last reply Reply Quote 1
                      17 out of 17
                      • First post
                        17/17
                        Last post
                      The Community of users of the Notepad++ text editor.
                      Powered by NodeBB | Contributors