in reply to Replacing with multiple occurrences.

Update: as stated before, I misread the question. There is no problem with Skeeve's solution, as it does what is stated.

Because I couldn't get Skeeve's to work on my strings, I did my own version, which should work:

my $string = 'aaabcdabcddddddefgg'; $string =~ s/((.)(?(?=\2{2,})\2+))\1/$1/g; print "$string\n";

I used the 'if 2 or more of the previous string' swallow them (prior to the backtrack, but anyhow), as it wouldn't work with duplicates when two or three were back to back, and * wasn't the solution, either.

Also to note, this 'rounds up', so if there are three back to back, two will remain (3 / 2 = 1.5, rounded up to 2).

Replies are listed 'Best First'.
Re: Re: Replacing with multiple occurrences.
by physi (Friar) on May 08, 2003 at 16:32 UTC
    As I understand the question, it was only for the pattern at the end of the string. So Skeeve's working very well.
    If you want to half every pattern in the string, just do:
    s/((.)\2*)\1/$1/g;
    That gives the same output like your does.

    -----------------------------------
    --the good, the bad and the physi--
    -----------------------------------
    
Re: Replacing with multiple occurrences.
by Skeeve (Parson) on May 09, 2003 at 06:37 UTC
    Quite complicated, isn't it? As physi already stated, a simple:
    s/((.)\2*)\1/$1/g
    will do the trick of half-ing each repeated occurence and rounding them up. So "aaabbbb" will become "aabb".