skip to Main Content

I have this lines :

_____0000_____
_____0000_____
_____0000_____
_____0000_____
.... 

I want to replace the 0000 with increment padded number ( incremented by 10) :

_____0010_____
_____0020_____
_____0030_____
_____0040_____
....
_____0090_____
_____0100_____
_____0110_____

2

Answers


  1. you can use the extension I wrote: Regex Text Generator

    • with Multi-Cursor select the 0000 part of every line.
      • you can do it with select first 0000 and then use Ctrl+D to select a few more
      • or select first 0000, Ctrl+F Alt+Enter
    • execute command: Generate text based on Regular Expression (regex)
    • as match regex use: .*
    • as generator regex use: {{=(i+1)*10:size(4)}}

    If you have blank lines and want to generate the whole text use:

    • place Multi-Cursor on all lines:
      • place cursor on first line
      • Shift+Alt+Click on last line
    • execute command: Generate text based on Regular Expression (regex)
    • as match regex use: .*
    • as generator regex use: _____{{=(i+1)*10:size(4)}}_____

    If you need to do this regular you can define a preset in the settings with these properties that are editable when selected.

    Login or Signup to reply.
  2. This is very easy with an extension I wrote, Find and Transform. With it you can select what you want to replace with a find regex and do the math to construct the replacement.

    Try this keybinding (in your keybindings.json or it could be a command):

    {
      "key": "alt+c",                    // whatever keybinding you want
      "command": "findInCurrentFile",
      "args": {
        "description": "replace 0000 with 0010, 0020, etc.",  // any description
    
        "find": "(?<=_____)(0000)(?=_____)",
        "isRegex": true,
    
        "replace": [         // replace with a javascript operation result
          "$${",
                             // make a String(...) so can use padStart next
            "let result = String(${matchNumber} * 10);",
            "return result.padStart(4, '0');",
          "}$$",
        ],
        "postCommands": "cancelSelection",  // to remove all the selections on each line
    
      },
    },
    

    There are many more options in the extension if you need them. Demo:

    replace selection with math operation demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search