• Login
Community
  • Login

Find/Replace number with Increment Value

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
12 Posts 4 Posters 1.9k 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.
  • D
    DJSpirosG
    last edited by Nov 9, 2020, 6:10 PM

    Hi to everyone, i am looking for some help here!!!

    What i want to do is to Search and Replace incrementally the pattern below [#n+1].
    As am aware this will require a python script probably.

    [#0]
    T=2
    F=INITIALKEY
    1=Gm
    2=1A
    3=0|0
    
    [#1]
    T=2
    F=INITIALKEY
    1=A\u266d m
    2=1A
    3=0|0
    
    [#2]
    T=2
    F=INITIALKEY
    1=D# m
    2=2A
    3=0|0```
    P 1 Reply Last reply Nov 9, 2020, 6:13 PM Reply Quote 0
    • P
      PeterJones @DJSpirosG
      last edited by Nov 9, 2020, 6:13 PM

      @DJSpirosG ,

      Yes, PythonScript is the way to go. There is even an example of a callback which does “add one” (search for add_1 in the PS “Editor Object” docs)

      P 1 Reply Last reply Nov 9, 2020, 6:14 PM Reply Quote 0
      • P
        PeterJones @PeterJones
        last edited by PeterJones Nov 9, 2020, 6:17 PM Nov 9, 2020, 6:14 PM

        …

        and here is a post where I show it in this Forum

        addenda: regex would be something like (\\[\#\d+\\]) /addenda

        D 1 Reply Last reply Nov 9, 2020, 6:40 PM Reply Quote 1
        • D
          DJSpirosG @PeterJones
          last edited by Nov 9, 2020, 6:40 PM

          @PeterJones thanks for the help!

          I’am burned…
          I don’t exactly understand how to succeed what i want to!
          Sorry!

          P 1 Reply Last reply Nov 9, 2020, 6:55 PM Reply Quote 0
          • P
            PeterJones @DJSpirosG
            last edited by Nov 9, 2020, 6:55 PM

            @DJSpirosG ,

            Sorry, given that you knew PythonScript would be required, I thought you already knew how to use it.

            if this is a one-off, just use the PythonScript, show console, and type the next 3 filled lines (plus a couple of extra returns between the function and the call, because the console requires an extra newline to end a function definition…)

            def add_1(m):
                return m.group(1) + str(int(m.group(2)) + 1) + m.group(3)
            
            
            editor.rereplace('(\[\#)(\d+)(\])', add_1);
            

            Yes, after debugging, I changed the regex to make the replacement easier for me.

            If this is going to be used more than once, do Python Script > New Script, and paste in those four lines, and use the PythonScript menus to run the script and/or assign it to the Run menu…

            D 1 Reply Last reply Nov 9, 2020, 7:35 PM Reply Quote 2
            • D
              DJSpirosG @PeterJones
              last edited by DJSpirosG Nov 9, 2020, 7:35 PM Nov 9, 2020, 7:35 PM

              @PeterJones If i am right this adds one to the existed digit inside the brackets.
              What i actually want is to add one to the next bracket so if bracket one is [#1] the next one to be [#2].

              The example below is my actual problem.
              i want the script to calculate the digit inside the previous bracket [#34] and replace the next one [#1] with [#35] the [#2] with [#36] and so on !

              [#34]
              T=2
              F=INITIALKEY
              1=E maj
              2=12B
              3=0|0
              
              [#1]
              T=2
              F=INITIALKEY
              1=Gm
              2=1A
              3=0|0
              
              [#2]
              T=2
              F=INITIALKEY
              1=A\u266dm
              2=1A
              3=0|0```
              P 1 Reply Last reply Nov 9, 2020, 7:55 PM Reply Quote 0
              • P
                PeterJones @DJSpirosG
                last edited by Nov 9, 2020, 7:55 PM

                @DJSpirosG ,

                I believe this does what you want:

                prev = None
                def renumber(m):
                    global prev
                    if prev == None:
                        prev = int(m.group(2))
                    else:
                        prev = prev + 1
                
                    return m.group(1) + str(prev) + m.group(3)
                
                editor.rereplace(r'^(\[#)(\d+)(\])', renumber);
                
                
                D 1 Reply Last reply Nov 9, 2020, 8:01 PM Reply Quote 1
                • D
                  DJSpirosG @PeterJones
                  last edited by DJSpirosG Nov 9, 2020, 8:03 PM Nov 9, 2020, 8:01 PM

                  @PeterJones Thank you for your time appreciated!

                  It needs a small fix (adding some \ ) and it works flawlessly!

                  editor.rereplace(r'^(\[\#)(\d+)(\])', renumber);
                  P 1 Reply Last reply Nov 9, 2020, 8:03 PM Reply Quote 0
                  • P
                    PeterJones @DJSpirosG
                    last edited by PeterJones Nov 9, 2020, 8:04 PM Nov 9, 2020, 8:03 PM

                    @DJSpirosG ,

                    Sorry, even in ```, the forum strips \\[ to become [ under certain circumstances
                    a3421931-2e2b-41ee-ac1c-2a54fcc2f2a2-image.png

                    D 1 Reply Last reply Nov 9, 2020, 8:05 PM Reply Quote 1
                    • D
                      DJSpirosG @PeterJones
                      last edited by Nov 9, 2020, 8:05 PM

                      @PeterJones
                      HaHa
                      i was so pissed of…
                      i was trying to post the correct one but i couldn’t…
                      i tried to edit my post about 100 times in the last minute!!!

                      Thanks once again!

                      1 Reply Last reply Reply Quote 0
                      • G
                        guy038
                        last edited by guy038 Nov 9, 2020, 8:26 PM Nov 9, 2020, 8:06 PM

                        Hello @peterjones, @DJSpirosG and All,

                        I suppose that the regex, in the Python script, is r'^(\\[#)(\d+)(\\])' and NOT r'^(\[#)(\d+)(\])' !!

                        If so, Peter, I can edit your post and make the corrections. Just tell me !

                        Remark : To explicitly write the literal strings \\[ and \\], in our forum, you need to write them as \\\[ and \\\]

                        Note also that, unfortunately, the Preview panel does not show the right layout :-(( Things are OK when you click on the Submit button, only !

                        BR

                        guy038

                        P.S. :

                        To write the text, of the Remark line, just above, I needed to write :

                        • First, 2 consecutive \ before each square bracket !

                        • Second, 3 consecutive \ before each square bracket !

                        1 Reply Last reply Reply Quote 3
                        • P
                          prahladmifour
                          last edited by Nov 10, 2020, 3:51 AM

                          This post is deleted!
                          1 Reply Last reply Reply Quote -5
                          7 out of 12
                          • First post
                            7/12
                            Last post
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors