Menu

Regex for add to found number

2008-07-05
2012-11-13
  • Nobody/Anonymous

    so, i want find 00, 01, 02, 03... and i want add +1 (or +2 etc.) for this number - so i want 01, 02, 03, 04... result after find/replace. is it possible in notepad++ find/replace tool?

    thanks

     
    • Fool4UAnyway

      Fool4UAnyway - 2008-07-05

      If each number is on a separate line, you may be helped by the Simple Script Plug-In's Increment function. You may need to download this plugin to be able to use it.

      Your script would simply say:

      >> start of script on next line
      Increment( 1, 1 )
      << end of script on previous line

      This means any number would be incremented by 1, allowing the length of numbers to grow by one digit (which, obviously, is enough).

      Preceding/Leading 0's will be preserved, except when they are "overwritten" by a 1 because of only 9's following it previously: 09(9..) will become 10(0..).

      If lines can contain more than one number, you have to separate them first. This is best done by using the advanced Replace dialog.

      Press Ctrl+Home to move to the top of the document.
      Press Ctrl+R to open the advanced replace dialog.

      Check the Regular Expression checkbox.

      In the Find field enter:
      (\d+[^\d]*)

      This regular expression searches for any number, separated from any succeeding number.

      In the Replace field enter:
      \1 [Press Ctrl+M after the 1]
      #NL#

      This expression re-places the number and the following text until the next number, adds a linebreak and the #NL# identifier to indicate the line wasn't broken here before.

      Find the first and replace all occurrences.

      Now you can run the simple script.

      After that, you'll have to re-connect the original lineparts again.

      Press Ctrl+Home to move to the top of the document.
      Press Ctrl+R to open the advanced replace dialog.

      Uncheck the Regular Expression checkbox.

      In the Find field enter:
      [Press Ctrl+M for the inserted linebreak]
      #NL#

      Clear the Replace field:

      Find the first and replace all occurrences.

      Done! Thank you very much, Fool4UAnyway! You're welcome!

      Here's the description of the function in the Simple Script Plug-In:

      >>Increment(int offset, int append): Increments by offset all lines as if they were numbers.

      Operates on each line in the document individually.

      The offset can be positive or negative.

      All of the digits in the line are treated collectively as a number, regardless of their position.  This number is incremented by the offset and inserted back into the string.

      If the resulting number has less digits than the original, 0's are inserted to fill in the missing places.

      If the resulting number has more digits than the original and append is 1, the extra digits are inserted before the first numeric character in the line.

      If append is 0, the extra digits are simply eliminated.

      If the resulting number is less than 0, the negative sign ("-") is treated as an extra digit.

      For example, if you have the following string:

      digits 5 and 0

      The results of the following scripts would be:

      increment(1,1)  =  digits 5 and 1

      increment(-2,1)  =  digits 4 and 8

      increment(10,1)  =  digits 6 and 0

      increment(50,1)  =  digits 10 and 0

      increment(-51,1) =  digits - and 1

      increment(-60,1) =  digits -1 and 0

      increment(50,0) =  digits 0 and 0

      increment(-60,0) =  digits 1 and 0