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

... more rigorously correct ... optimization ...

For me, the issue is not rigor or optimization, but correctness. Insofar as I understand it, the OP requires an overall match on more-than-n contiguous identical characters (although an n-character match may be a component of the overall match). The regexes to which I object match equal-to-or-more-than-n such characters overall, and so are incorrect. The incorrectness of the matches is obscured, but not ameliorated, by the subsequent substitutions, which may replace a string S (which may be empty) with an identical string S.

The point can be exemplified by looking at what is matched by the  qr{ (.)\1\1 \K \1* }xms regex (the most elegant, IMHO, in this thread) in a context that does not involve substitution. (Similar examples could be constructed for the other objectionable regexes.) All the empty strings are, in my view, unneeded (and improper) matches.

>perl -wMstrict -le "my $s = 'aaa...bbbb...ccccc...dddddd.x..yy'; print qq{'$2'} while $s =~ m{ (.)\1\1 \K (\1*) }xmsg; " '' '' 'b' '' 'cc' '' 'ddd'

Anyway, that's my story and I'm sticking to it.

Replies are listed 'Best First'.
Re^3: Supress similar chars in the string
by kennethk (Abbot) on Jan 19, 2011 at 20:51 UTC

    I would politely disagree with your interpretation, as the two approaches are functionally equivalent. There was no mention of side effects/counting in the OP, and so judging the proposed solution on those grounds is inappropriate. It is an inferior algorithm and likely a bad habit, but the code does the job and does it in a straight-forward fashion. If I'd spent more brain power considering the question, I would have substituted + for *. The modification actually occurred to me after I posted but before I'd read your comments. However, I try to avoid correcting posts for stylistic reasons alone to avoid versioning confusion and race conditions.

    I agree that the \K construct is the most elegant and appropriate solution, but not everyone runs 5.10 - my external server runs 5.8. The alternate choice option I list seems the most transparent for a neophyte to my eye, but I expect to be the the most expensive given the e modifier.