-2

I've certain texts. I am trying to replace the numbers inside the [] with number starting from 1 then in incremental fashion. Is this possible using regex search and replace feature in notepad++.

pig[1]
chicken[2]
monkey[3]
duck[7]
goat[4]
buffalo[5]

Output >>

pig[1]
chicken[2]
monkey[3]
duck[4]
goat[5]
buffalo[6]
user4221591
  • 2,084
  • 7
  • 34
  • 68
  • 1
    Guys please can you give me the reason of negative marking? If no valid reason please remove the negative marking – user4221591 Dec 16 '15 at 09:36
  • @DeanTaylor: That isn't a real duplicate because the digits to be replaced aren't on the same column. – Toto Dec 16 '15 at 10:35
  • Don't know about notepad++, but you can solve it easily on the command line, e.g. with this line of powershell: `$counter = 0; get-content .\input.txt | %{ $counter += 1; $_.Split('[')[0] + "[$counter]"; }` – steinar Dec 16 '15 at 15:27

1 Answers1

3

You can do the job in two steps:

  1. First step
    • Move the cursor at the begining of the first line
    • Select, in Edit menu, Edit in column mode (I'm not sure of the label, because I don't have an english version) you may type  Alt+C
    • In the pop-up window, choose the initial number and the increment then click OK
  2. Second step:
    • Ctrl+H
    • Find what: (\d+)(.+?)\[\d+\]
    • Replace with: $2[$1]
    • then click on Replace all

Regex explanation:

(       : Start group 1
  \d+   : 1 or more digits
)       : End group 1
(       : Start group 2
  .+?   : 1 or more any character except linebreak non greedy
)       : End group 2
\[\d+\] : 1 or more digits enclosed in brackets

Replacement part:

$2      : Content of group 2 (ie. word before the opening braket: pig, chicken, ...)
[$1]    : Content of group 1 (ie. the number generated in step 1), enclosed in brackets
Toto
  • 89,455
  • 62
  • 89
  • 125
  • Thank you very much for the reply. Please can you explain the regex you have used, I mean how does it work. `(\d+)(.+?)\[\d+\]` and `$2[$1]`. Thank you !!! – user4221591 Dec 17 '15 at 00:23