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

Hello brothers,

I have a string that looks like "aabbxxcccdadenmmpyyx". I want to find every pair of identical characters (e.g. aa) and put an 'x' between them (i.e. axa).

With a few milliseconds worth of thought, I came up with the following:

$str =~ s/(.)\1/$1x$1/g;

This works for double characters -- HOWEWER, the string shown above, once processed, looks like:

"axabxbxxxcxccdadenmxmpyxyx"

Note the ccc in the original turned to cxcc, whereas I wanted cxcxc. The problem here is that the regexp "cursor" points to the third c when it had matched the cc pair. It process the cc pair to cxc, and continued on its merry old way starting with the third c.

Any clues how to solve this interesting problem using a regexp (and not resorting to popping characters off the string using substr or the like).

Cheers
Andy
--

Replies are listed 'Best First'.
Re: Matching double characters and insertion
by japhy (Canon) on Jul 20, 2001 at 06:00 UTC
    Use a look-ahead to make sure you COULD match the repeated letter, but not actually advance in the string:
    $str =~ s/(.)(?=\1)/$1x/g;

    _____________________________________________________
    Jeff japhy Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Matching double characters and insertion
by Masem (Monsignor) on Jul 20, 2001 at 06:00 UTC
    Sometimes regex isn't the answer to all problems. I see a better solution using straightforward perl to get around the problem of how regex's work. (Mind you, there might be a regex solution, but here, I know this works ;-)
    my @array = split //, $string; my @newarray; my $c = shift @array; while ( @array ) { my $d = shift @array; push @newarray, $c; if ( $c eq $d ) { push @newarray, 'x'; } $c = $d; } push @newarray, $c; $string = join '', @newarray; #update
    ----------------------------------------------------- Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Only if you join() @newarray.

      _____________________________________________________
      Jeff japhy Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Matching double characters and insertion
by John M. Dlugosz (Monsignor) on Jul 20, 2001 at 08:55 UTC
    Or, adjust pos() back by one before trying again, or use \G for similar effect.
Re: Matching double characters and insertion
by abstracts (Hermit) on Jul 20, 2001 at 14:26 UTC
    Hello,

    You can also do:

    $str =~ s{((.)\2*)}{join'x',split(//,$1)}eg;
    
    Hope this helps,,,

    Aziz,,,

Re: Matching double characters and insertion
by Sifmole (Chaplain) on Jul 20, 2001 at 16:18 UTC
    This is not exactly a response to your question, but I just wanted to mention it anyway.

    You seem to have a LOT of Regex questions, are you aware of the book "Mastering Regular Expressions"? It is a terrific book. Obviously, the monks don't mind giving assistance as can be seen by the amount of good answers you are getting; However, getting ahold of, and reading this book might really illuminate some things for.

    Just passing along some extra information about how to fish....