Need regex for incremental by 1



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



  • @Scott-Sumner

    Thanks very much!

    I will do this way…



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



  • @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 ?



  • 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



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

Log in to reply