-1

I'm stuck trying to figure how to implement an idea to my files, my idea is to change numbers to the next one in order, so:

  • 0 to 1
  • 1 to 2
  • 3 to 4 and so on.

This can easily be done with find and replace boxes, but the problem is if I start finding and replacing, the numbers I just increased will change again, I know this kinda doesn't make sense but I need a regex that changes all numbers in the file once to the next number in line

So let's say a line has a 0 and I change it to a 1, I need it to stay a to a 1 and not a 2 when I find and replace 1 to 2, I don't know if I'm explaining myself please anyone help

Examples

Order66
fight01
fly45

I need to be

Order77
Flight12
fly56

If I keep changing them manually with find and replace, they will turn out way different since I keep finding and replacing, that's why I need one regex to do it all at once

ctwheels
  • 21,901
  • 9
  • 42
  • 77

1 Answers1

4

Brief

In Notepad++ you can use conditional replacements with regex. See this post by Wiktor for more information.


Code

(0)|(1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9)

Replacement

(?{1}1)(?{2}2)(?{3}3)(?{4}4)(?{5}5)(?{6}6)(?{7}7)(?{8}8)(?{9}9)(?{10}0)

Results

Input

Order66
fight01
fly45

Code before regex replace

Output

Below output after clicking on Replace All in Notepad++

Order77
Flight12
fly56

Code after regex replace


Explanation

  • Each digit is enclosed in its own group i.e. (0).
    • Each group is given a different identifier. i.e. (0) is group 1
  • In the replacement we can conditionally target groups that contain values and, as such (?{1}1) targets the first capture group and replaces its contents with 1 ((?{1}) references capture group 1)
ctwheels
  • 21,901
  • 9
  • 42
  • 77