You can't count in regexes, unless you actually call code blocks. This can be done either in the substitution part with s/.../.../e or in the regex with (?{ code }) assertions.

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...


In reply to Re: Indexing multiplicate firstnames with regex by moritz
in thread Indexing multiplicate firstnames with regex by stringZ

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.