http://www.perlmonks.org?node_id=1047388


in reply to repeated characters in regular expressions

G'day AmnonM,

Welcome to the monastery.

If you decrement the 'n' value, you can do this:

s/(.)\1{$n}/$1 x $m/eg;

Here's my test:

$ perl -Mstrict -Mwarnings -E ' sub mod_char_count { my ($n, $m, $x) = @{+shift}; --$n; $x =~ s/(.)\1{$n}/$1 x $m/eg; return $x; } my @data = ( [4, 1, "p8888er9999l"], [1, 3, "monks"], ); say mod_char_count($_) for @data; ' p8er9l mmmooonnnkkksss

-- Ken