• Login
Community
  • Login

Need regex for incremental by 1

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
8 Posts 3 Posters 11.5k 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.
  • G
    ganesan govindarajan
    last edited by ganesan govindarajan Sep 11, 2018, 11:18 AM Sep 11, 2018, 11:16 AM

    Hi all,

    I have the texts like "indent=“0”, indent=“1”, indent=“0”, indent=“2” etc in several places in the xml file.

    I wanted to replace with incremental by 1 in to the each value as indent=“1”, indent=“2”, indent=“1”, indent=“3” etc.

    Is there any possible way available in notepad++?

    thanks
    ganesang

    S 1 Reply Last reply Sep 11, 2018, 12:11 PM Reply Quote 0
    • S
      Scott Sumner @ganesan govindarajan
      last edited by Sep 11, 2018, 12:11 PM

      @ganesan-govindarajan

      There is no way to do it within Notepad++ without scripting. See the following:

      • https://notepad-plus-plus.org/community/topic/15318/replace-text-with-incremented-counter
      • https://notepad-plus-plus.org/community/topic/12967/replace-string-across-multiple-files-and-increment-value
      S 1 Reply Last reply Sep 11, 2018, 12:53 PM Reply Quote 0
      • S
        Scott Sumner @Scott Sumner
        last edited by Sep 11, 2018, 12:53 PM

        @Scott-Sumner said:

        no way to do it within Notepad++

        Well…it depends upon how much effort you want to expend…

        Say you used the Edit (menu) -> Column Editor… feature (twice) to create a two column table of incrementing numbers (it could be a long table, I just show a short one):

        0 1
        1 2
        2 3
        3 4
        ...
        

        and then you put a line of dashes (or any sequence of characters that does not appear in your data) and this table AFTER your data in your Notepad++ tab:

        I have the texts like "indent="0", indent="1", indent="0", indent="2" etc in several places in the xml file.
        -----
        0 1
        1 2
        2 3
        3 4
        

        Then a regex replacement could solve this problem:

        Invoke Replace dialog (default key: ctrl+h)
        Find what zone: (?-i)indent="(\d+)"(?s)(?=.*?^-----.*?^\1\h+(\d+))
        Replace with zone: indent="\2"
        Wrap around checkbox: ticked
        Search mode selection: Regular expression
        Action: Press Replace All button

        Results:

        I have the texts like "indent="1", indent="2", indent="1", indent="3" etc in several places in the xml file.
        -----
        0 1
        1 2
        2 3
        3 4
        

        After that you simply remove the line of dashes and the table…

        G 1 Reply Last reply Sep 12, 2018, 6:13 AM Reply Quote 2
        • G
          ganesan govindarajan @Scott Sumner
          last edited by Sep 12, 2018, 6:13 AM

          @Scott-Sumner

          Thanks very much!

          I will do this way…

          1 Reply Last reply Reply Quote 1
          • V
            Vasile Caraus
            last edited by Sep 13, 2018, 11:28 AM

            ok, and another exemple.

            If I have 100 .html pages, and in each page I have such as:

            page 1:
            $item_id = 0001;
            page 2:
            $item_id = 0002;
            page 3:
            $item_id = 0003;

            How can I replace with incremental by 1 in to the each value, in order to obtain:

            page 1:
            $item_id = 0002;
            page 2:
            $item_id = 0003;
            page 3:
            $item_id = 0004;

            etc…

            S 1 Reply Last reply Sep 13, 2018, 12:00 PM Reply Quote 0
            • S
              Scott Sumner @Vasile Caraus
              last edited by Sep 13, 2018, 12:00 PM

              @Vasile-Caraus said:

              100 .html pages, and in each page

              What is the meaning of “page” in this context…do you have 100 html files and you want to do this operation on each file ?

              1 Reply Last reply Reply Quote 1
              • V
                Vasile Caraus
                last edited by Sep 13, 2018, 12:14 PM

                I have 100 .html pages, and each page has a unique string. For exemple, page 1 has $item_id = 0001;, page 2 has $item_id = 0002; etc…

                I want to replace in each file with incremental by 1 in to the each value

                S 1 Reply Last reply Sep 13, 2018, 12:46 PM Reply Quote 0
                • S
                  Scott Sumner @Vasile Caraus
                  last edited by Sep 13, 2018, 12:46 PM

                  @Vasile-Caraus

                  It sure seems like you could use a variation on the technique described above:

                  • key concept: use Replace All in All Open Documents or Replace in Files for most of the action
                  • create a delimiter and a lookup-table running from 0 to 101 (or whatever), probably all on one line is best, e.g. \r\n-----0:1,1:2,2:3,3:4,4:5,5:6,6:7,7:8,8:9,9:10,10:11,… ; you can do this with the column-editor features to ease the pain…followed by some editing…
                  • regex replace \z in each file with the lookup table string created above (this will add the table at the end of each file)
                  • run a regex replacement (on all files) like described above for the other poster but customized for your data, e.g. \$item_id = \d+; and the contents of the lookup-table string
                  • craft another regex replacement to remove the delimiter and lookup-table from all files
                  1 Reply Last reply Reply Quote 1
                  4 out of 8
                  • First post
                    4/8
                    Last post
                  The Community of users of the Notepad++ text editor.
                  Powered by NodeBB | Contributors