Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Need regex for incremental by 1

    Help wanted · · · – – – · · ·
    3
    8
    7036
    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.
    • ganesan govindarajan
      ganesan govindarajan last edited by ganesan govindarajan

      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

      Scott Sumner 1 Reply Last reply Reply Quote 0
      • Scott Sumner
        Scott Sumner @ganesan govindarajan last edited by

        @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
        Scott Sumner 1 Reply Last reply Reply Quote 0
        • Scott Sumner
          Scott Sumner @Scott Sumner last edited by

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

          ganesan govindarajan 1 Reply Last reply Reply Quote 2
          • ganesan govindarajan
            ganesan govindarajan @Scott Sumner last edited by

            @Scott-Sumner

            Thanks very much!

            I will do this way…

            1 Reply Last reply Reply Quote 1
            • Vasile Caraus
              Vasile Caraus last edited by

              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…

              Scott Sumner 1 Reply Last reply Reply Quote 0
              • Scott Sumner
                Scott Sumner @Vasile Caraus last edited by

                @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
                • Vasile Caraus
                  Vasile Caraus last edited by

                  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

                  Scott Sumner 1 Reply Last reply Reply Quote 0
                  • Scott Sumner
                    Scott Sumner @Vasile Caraus last edited by

                    @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
                    • First post
                      Last post
                    Copyright © 2014 NodeBB Forums | Contributors