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
--
In reply to Matching double characters and insertion by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |