in reply to Indexing multiplicate firstnames with regex
But all in all it seems foolish to do that kind of stuff in a single regex, because it's not what they are made for; hashes and a loop are a much better fit for this situation.
Update: nonetheless I tried to write a regex which does the substitution, but it only works for the first name unless you use it in a loop. Here it goes:
use strict; use warnings; my $str = join '', <DATA>; my $i = 0; 1 while $str =~ s{ ^((?>\w+))(?!\d)(.*?) ^\1(?!\d) }{"$1$2$1" . ++$i}msxeg; print $str; __DATA__ George Fortran Jessi Heavens Bill Clinton Barack Obama Bill Gates Steve Jobs Bill Green George Bush
Second update: Uhm, I didn't read it too carefully, the code doesn't do what it should; ignore it...
|
|---|