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

I have array containing many words. I just want those words out of that array which contain same characters side by side e.g 'aa', 'bb', '11', '!!', 'zz' etc

So I want the regular expression that should be able to match only words of following type:

Google, Bee, Dell, Linux99, s@@

Replies are listed 'Best First'.
Re: Match 2 same characters
by mr_mischief (Monsignor) on Mar 19, 2009 at 18:22 UTC
    So what you want is a regular expression with a backreference.

    my @array = qw( foo bar baz quux xyzzy); my @matched; for my $elem ( @array ) { push @matched, $elem if $elem =~ m/(.)\1/; } print "$_\n" for @matched;

    See perlre and search for "backreference" or for "find first doubled" for more explanation.

Re: Match 2 same characters
by kennethk (Abbot) on Mar 19, 2009 at 18:09 UTC
    Might I suggest that a C-style for loop combined with substr might be a more reasonable approach, especially since you want to match every character under the sun?

    Update: The literal solution could be obtained using /(.)\1/ (see perlre).

Re: Match 2 same characters
by bichonfrise74 (Vicar) on Mar 19, 2009 at 18:50 UTC
    Hi,

    A simple array for-loop can do the trick...
    #!/usr/bin/perl use strict; my @old_arrays = qw( aa aa bb bb cc dd ); my @new_arrays; push( @new_arrays, $old_arrays[0] ); for (my $i = 0; $i <= $#old_arrays; $i++) { push( @new_arrays, $old_arrays[$i] ) if ( ( $old_arrays[$i] ne $ol +d_arrays[$i-1] ) && $i > 0 ); } print "@new_arrays\n";

      A grep might be a bit simpler. I'm not sure why you push the first element outside of the loop and then have to test for element number inside. It seems to be an un-necessary complication.

      use strict; use warnings; my @pets = qw{ gerbil parrot hamster cat poodle goldfish }; my @doubles = grep m{(.)\1}, @pets; print qq{$_\n} for @doubles;

      The output

      parrot poodle

      I hope this is of interest.

      Cheers,

      JohnGG

        Hi JohnGG,

        I totally misunderstood the question. Hehehe...
Re: Match 2 same characters
by paragkalra (Scribe) on Mar 19, 2009 at 18:59 UTC
    Sorry for the typo. I was trying: if ( $_ =~ /(.){1}
      {1} is a quantifier. It modifies what the preceding atom ((.) in this case) matches. This and other quantifiers are documented in perlre.
Re: Match 2 same characters
by paragkalra (Scribe) on Mar 19, 2009 at 18:56 UTC
    Thanks mr_mischief. It worked like a charm

    BTW I was trying: if ( $_ =~ /(.)\1/ )

    What was wrong that
Re: Match 2 same characters
by paragkalra (Scribe) on Mar 19, 2009 at 18:19 UTC
    Can't it be achieved through regular expressions?