regexreplacesequences

Insert Sequential Number using Regular Expression


Given:

pad_
pad_
pad_

How would it be possible to use a regular expression to act like a "for loop" to obtain:

pad_1
pad_2
pad_3

I am aware that numbered sequences such as \1 and \2 can be used in replacement expressions to indicate () captures in existing text, but can a single replacement expression generate its own replacement values as a numerical sequence? Is there some kind of trick to make this happen besides keeping on-hand a text file with a sequence of numbers that I can copy/paste, or writing a quick application that outputs text using a "for loop"?

CONCLUSION: I get the impression that the regular expression syntax is not a standard uniformly implemented by text editing applications, nor does it include anything that meets the purpose of utilizing match counts in replacement expressions or initiating replacement text sequences. Unfortunately, that means that I would need to use text editors like vim that could do this for purposes unsuited to them, or attempt to find or make application plugins for the text editor or IDE that I would be using. But it would actually be much simpler to compile and run a quick "for loop" that prints the strings for me to copy/paste and which I can reuse by altering in minor ways, or to use awk which was suggested.


Solution

  • if perl is ok, I guess awk would be ok too:

    awk '$0=$0 NR' file
    

    I don't have notepad++, but it is an easy job for vim.

    one way with vim editor

    It looks like:

    enter image description here

    another way with vim editor

    enter image description here