Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

repeated characters in regular expressions

by AmnonM (Initiate)
on Aug 01, 2013 at 07:25 UTC ( #1047379=perlquestion: print w/ replies, xml ) Need Help??
AmnonM has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I want to look through a string, and if I find n repeated characters, I want to replace them with m of the same character. Probably a simple regexp for a monk?

e.g., if n=4 and m=1 then... p8888er9999l --> p8er9l

if n=1 and m=3 monks --> mmmooonnnkkksss

Thanks

Comment on repeated characters in regular expressions
Re: repeated characters in regular expressions
by Utilitarian (Vicar) on Aug 01, 2013 at 07:54 UTC
    Something like the following $string=~s/(.){$n}/$1 x $m /eg;

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
      The (.){$n} construct will match any sequence of $n characters, not just repeating characters.
Re: repeated characters in regular expressions
by Loops (Hermit) on Aug 01, 2013 at 07:57 UTC

    The below relies on embedding Perl code in the substitution portion of the regex (/e) and could also be a little more terse if you can guarantee a newer version of Perl, where the /r parameter of regex is available.

    sub translate { my ($n, $m, $_) = @_; $n--; s/(\w)\1{$n}/$1 x $m/ge; $_; } print translate(4,1,'p8888er9999l'); # -> p8er9l print translate(1,3,'monks'); # -> mmmooonnnkkksss
Re: repeated characters in regular expressions
by kcott (Abbot) on Aug 01, 2013 at 08:03 UTC

    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

      Thanks to all. I'll try these implementations.
Re: repeated characters in regular expressions
by jwkrahn (Monsignor) on Aug 01, 2013 at 09:07 UTC
    $ perl -le' my $n = 4; my $m = 1; $_ = "p8888er9999l"; print; s/((.)\2*)/ $n == length $1 ? $2 x $m : $1 /eg; print; ' p8888er9999l p8er9l $ perl -le' my $n = 1; my $m = 3; $_ = "monks"; print; s/((.)\2*)/ $n == length $1 ? $2 x $m : $1 /eg; print; ' monks mmmooonnnkkksss

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047379]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2014-07-31 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls