in reply to Re: Supress similar chars in the string
in thread Supress similar chars in the string

And if the OP ever had the need to scale it up to more than 3 characters:
use strict; use warnings; my $n = 3; # number of adjacent characters $n--; $_ = 'testing 1234567 ........ aaaaaaaasssssss __________ ++++++++++ - +-------- testing testing '; s/((.)\2{$n})\2*/$1/g; print;

Replies are listed 'Best First'.
Re^3: Supress similar chars in the string
by Jim (Curate) on Jan 18, 2011 at 18:50 UTC

    Is this a case where adding the /o modifier to the s/// operator might be helpful? It's an efficiency measure intended to ensure (or help ensure, I think) the regex is only compiled once.

    s/((.)\2{$n})\2*/$1/go;
      Is this a case where adding the /o modifier to the s/// operator might be helpful?

      No, /o is mostly misused and obsolete , see //o of any help?

Re^3: Supress similar chars in the string
by AnomalousMonk (Archbishop) on Jan 18, 2011 at 22:26 UTC
    s/((.)\2{$n})\2*/$1/g;

    Shouldn't the  \2* (zero or more of...) term in the regex above be  \2+ (one or more of...) instead? I.e.:
        s/((.)\2{$n})\2+/$1/g;
    Won't the  \2* version lead to useless replacement of sub-strings of n identical characters with the same n-character sub-string (Update: where n is the maximum number of contiguous identical characters originally defined)?

    Update: This really should have been posted as a reply to Re: Supress similar chars in the string.