Notepad++ Find/Replace number with Increment Value

2019-01-08 00:15发布

问题:

Is it possible in Notepad++ to find a number, then replace it with increment value?

For example, find id number: regex \((\d+)

INSERT INTO `wp_make`(`id`, `name`, `slug`) VALUES (0,"audi","audi");
INSERT INTO `wp_make`(`id`, `name`, `slug`) VALUES (1,"BMW","bmw");
INSERT INTO `wp_make`(`id`, `name`, `slug`) VALUES (2,"Mercedes","mercedes");

replace with id + 31: how to?

INSERT INTO `wp_make`(`id`, `name`, `slug`) VALUES (31,"audi","audi");
INSERT INTO `wp_make`(`id`, `name`, `slug`) VALUES (32,"BMW","bmw");
INSERT INTO `wp_make`(`id`, `name`, `slug`) VALUES (33,"Mercedes","mercedes");

回答1:

It is not possible with just a regex, but you can use a python script inside Notepad++.

Here are the steps:

  • Install Python Script 1.0.8.0
  • Go to the Plugins -> Python Script -> New Script
  • Select the file name (say, "increment_numbers.py")
  • Place this script there:

Code:

def increment_after_openparen(match):
    return "({0}".format(str(int(match.group(1))+31))

editor.rereplace(r'\((\d+)', increment_after_openparen)

Then, just evoke this 'increment_numbers' script.