typhoonmentat has asked for the wisdom of the Perl Monks concerning the following question:

Monks, how would I go about matching a character already matched a second time? For instance, the code:

/^.+?\w{2}

can match any two alphanumeric characters, but what if I want to match "aa" or "bb" or "zz"? I'm really looking for some code that will match when a word has three double-letter pairs in it, like "hullaballoo".

Thanks, Rob

demerphq editted, changed pre content to normal html

Replies are listed 'Best First'.
Re: Repeated character in regex
by bart (Canon) on Mar 19, 2005 at 11:54 UTC
    Look into \1 and related, the backreference equivalents for $1 etc. /(\w)\1/ will match repeated letters (and digits and underscore), and /(\w)\1/i will do that case insensitively, thus the former will math "ww" or "WW" but not "wW", while the latter will match "wW" too.

    For more on this, check out the classic article by Tom Christiansen, FMTEYEWTK about regular expressions.

    p.s. Make sure that you backreference number agrees with you used capturing parens. If you want to wrap parens around the whole match, you'll have to replace the \1 with \2:

    /((\w)\2)/
    BTW To know the correct number to use, just count the opening parens. So the outer parens are number 1, the inner parens are number 2.
Re: Repeated character in regex
by holli (Abbot) on Mar 19, 2005 at 11:53 UTC
    backreferences:
    my $s = "hullaballoo"; while ( $s =~ /(\w)\1/g ) { print "doubled: $1\n"; }


    holli, /regexed monk/
Re: Repeated character in regex
by ysth (Canon) on Mar 21, 2005 at 04:26 UTC
    But don't try /(\w)\1.*?(\w)\2.*?(\w)\3/; it could take a while. Use (()=/(\w)\1/g) >= 3 or some such.
Re: Repeated character in regex
by tphyahoo (Vicar) on Mar 21, 2005 at 13:47 UTC
    you could use "scalar" to get how many matches were returned by =~ into a @matches array....
    while (<DATA>) { chomp; my $s = $_; @matches = $s =~ /(\w)\1/g; if (scalar @matches == 3) { print "$s: three two-letter repeats."; } } __DATA__ hullaballoo chubbalicious