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 | |
|
Re: Matching double characters and insertion
by Masem (Monsignor) on Jul 20, 2001 at 06:00 UTC | |
by japhy (Canon) on Jul 20, 2001 at 06:05 UTC | |
|
Re: Matching double characters and insertion
by John M. Dlugosz (Monsignor) on Jul 20, 2001 at 08:55 UTC | |
|
Re: Matching double characters and insertion
by abstracts (Hermit) on Jul 20, 2001 at 14:26 UTC | |
|
Re: Matching double characters and insertion
by Sifmole (Chaplain) on Jul 20, 2001 at 16:18 UTC |