Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: repeated characters in regular expressions

by Loops (Curate)
on Aug 01, 2013 at 07:57 UTC ( #1047385=note: print w/ replies, xml ) Need Help??


in reply to repeated characters in regular expressions

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


Comment on Re: repeated characters in regular expressions
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2015-07-29 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (268 votes), past polls