-1

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.

Sparky
  • 172
  • 15
  • what tool/editor do you use? – Kent Feb 06 '18 at 12:01
  • I would be using Notepad++ or TextWrangler. Perl regular expressions are fine. This is useful: https://stackoverflow.com/questions/23426617/textwrangler-grep-regexpression-reference#23428975 – Sparky Feb 06 '18 at 12:03

2 Answers2

4

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

  • %s/$/0/ This append the 0 to each line
  • select all lines, you can do ggVG
  • let the number grow: gCtrl-A

It looks like:

enter image description here

another way with vim editor

  • visual select (V for example) the lines you want to append seq
  • :'<,'>s/$/\=line('.')-line("'<")+1 when you press : the '<,'> range will be filled automatically
  • Press Enter, done.

enter image description here

Kent
  • 189,393
  • 32
  • 233
  • 301
  • Thanks for the suggestion. Is there an approach besides using a separate program, but only using a regular expression? Perhaps I could somehow get the match count and use that as a replacement value. – Sparky Feb 06 '18 at 12:12
  • If you give an example of how I could do this using vim regular expressions, I'll mark your answer as a solution because it technically would be achieved within the text editor. I looked at http://www.vimregex.com/ but could not see how it would be an easy job for vim to insert sequential numbers, as you said. Please clarify with exactly what I should enter into vim to make this happen. – Sparky Feb 06 '18 at 12:36
  • 1
    I can give more than one example, how can you achieve your goal in vim editor., however, how to do it only with pure regex, without the vim editor power. I don't know. @Sparky – Kent Feb 06 '18 at 12:43
  • Please note the conclusion that I edited into my question. I'll probably end up using `awk` or a quick separate program which uses a "for loop" that outputs text. – Sparky Feb 06 '18 at 12:58
  • I'm a newbie at vim, so I couldn't figure out how to do the "let the number grow" step you mentioned in the first example you provided. `v` starts visual mode and I use `shift-G` and the arrow keys to select all the text, but the next key sequence stumps me. The second example works well, but how would I match `pad_` lines interspersed among other lines using a single command to maintain the sequential numbering? – Sparky Feb 06 '18 at 13:15
0

TextPad https://textpad.com/index.html

TextPad will do this. It has a built in function that increments a counter \i

\i starts from 1 and increments by 1

\i(100) starts from 100 and increments by 1

\i(100,10) starts from 100 and increments by 10

etc.

Example regular expression

  • I wanted to add a question number to each question in an aiken file (I found the answer to my question below and I remembered my login details) – Rupert Russell May 16 '20 at 19:20