regexpowershellnotepad++

How do I renumber the numbers of this superscript?


How do I renumber the numbers of the following superscript in a text document? :-

Hfghh<sup>[1]</sup>. Hfgghc<sup>[1]</sup>. Fhvfh<sup>[2]</sup>. Dfyhh<sup>[3]</sup>. Xfghg<sup>[4]</sup>. Rfchh<sup>[5]</sup>. Ffhvy<sup>[6]</sup>. Xfhhb<sup>[7]</sup>. Tfhfh<sup>[8]</sup>. Igxfg<sup>[9]</sup>. Atgvvh<sup>[10]</sup>. Zfthb<sup>[11]</sup>. Phfdb<sup>[12]</sup>.

The output should be as follows:-

Hfghh<sup>[1]</sup>. Hfgghc<sup>[2]</sup>. Fhvfh<sup>[3]</sup>. Dfyhh<sup>[4]</sup>. Xfghg<sup>[5]</sup>. Rfchh<sup>[6]</sup>. Ffhvy<sup>[7]</sup>. Xfhhb<sup>[8]</sup>. Tfhfh<sup>[9]</sup>. Igxfg<sup>[10]</sup>. Atgvvh<sup>[11]</sup>. Zfthb<sup>[12]</sup>. Phfdb<sup>[13]</sup>.

That is, the numbering should start from "1" and incrementally increase by "1", without any repetition of numbers. I have been doing it one by one but that is cumbersome since there are more than a hundred of them.

I understand how to use Notepad++ but that too involves many steps to get the desired output and I am looking for something less cumbersome. The method I used with the help of Notepad++ was as follows:

  1. Open this above mentioned text document in Notepad++ and press Ctrl+H,
  2. Select the regular expression mode and type <sup>\[\d+\]<\/sup> in the "Find" field and <sup>[]</sup> in the "Replace" field and hit "Replace All" which removes all the numbers.
  3. Type <sup>\[\]<\/sup> in the "Find" field and <sup>[\r\n]</sup> in the "Replace" field and hit "Replace All" (still with the regular expression mode selected) to put the ]</sup> on the next line.
  4. Put the cursor just before the first ]</sup> and keep pressing Alt+Shift+ till you reach the last ]</sup>.
  5. Press Alt+C and in the new pop-up, select the "number to insert" option, then the "Dec (decimal)" option below and type "1" where it asks for the, "Initial number", "1" where it asks for the, "Increase by" and "1" where it asks for the, "Repeat" and hit "OK" which will add the numbers incrementally but with an extra, unwanted space after the numbers 1 to 9.
  6. Type <sup>\[\R(\d+) in the "Find" field and <sup>[$1 in the "Replace" field and hit, "Replace All" which puts what was on the next line as the <sup>[ on the same line as the <sup>[.
  7. Type <sup>\[(\d+)\s*\] in the "Find" field and <sup>[$1] in the "Replace" field and hit, "Replace All" to get rid of the spaces after the numbers.

That gives me (or you) the desired output (superscript numbers starting with "1" and incrementally increased by "1", without any repetition of numbers), but it is cumbersome and I want a shorter, less cumbersome process to do the same.

The answer posted at vim, is it possible to add increasing numbers next to words each time the words are repeated in a text? seems good but I don't understand the script nor how to use it.


Solution

  • For the benefit of future readers:-

    @Toto's answer may replace other numbers as well, so I am posting another PythonScript here to match <sup>, followed by a [ bracket and then the number sought as per the question. You can run a python script within the PythonScript plugin of Notepad++ for that.

    If it is not yet installed, install it via Plugins Admin. To install the PythonScript plugin in Notepad++, navigate to Plugins > Plugins Admin. Check the box next to PythonScript and click Install. Notepad++ will restart, and after that, you can find the plugin in the Plugins menu.


    # encoding=utf-8
    from Npp import *
    
    def custom_replace_func(m):
        new_text = "<sup>\[{}".format(custom_replace_func.counter)
        custom_replace_func.counter += custom_replace_func.increment
        return new_text
    
    custom_replace_func.counter = 1  # starting value
    custom_replace_func.increment = 1
    editor.rereplace(r'<sup>\[(\d+)', custom_replace_func)