Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an updating match counter which I can use as a prefix in the replace text?

Tags:

regex

perl

In a perl regex, is there an updating match counter which I can use as a prefix in the replace text? For example, I want to replace each occurrence of position with position 1, position 2 and so on.

like image 672
Newbie EK Avatar asked Dec 05 '25 02:12

Newbie EK


1 Answers

No, there is no predefined counter, but you can easily create one yourself:

my $i = 1;
s/position/ "position " . $i++ /eg;

(Using the /e flag to tell perl to parse the replacement part as a block of code, not a string.)

like image 186
melpomene Avatar answered Dec 06 '25 17:12

melpomene