Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: how to replace a matched pattern in a string with a different pattern of same length?

by wind (Priest)
on Jun 20, 2011 at 22:18 UTC ( #910633=note: print w/replies, xml ) Need Help??


in reply to how to replace a matched pattern in a string with a different pattern of same length?

First calculate the longest using List::Util->max and then do a substitution.
use List::Util qw(max); use strict; use warnings; my $string = '00111100110'; my $longest = max map {length} $string =~ /(1+)/g; $string =~ s/1{$longest}/'2' x $longest/eg; print $string, "\n";
  • Comment on Re: how to replace a matched pattern in a string with a different pattern of same length?
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://910633]
help
Chatterbox?
[Discipulus]: good morning, if raining here
[choroba]: Good morning!

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2017-04-28 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (519 votes). Check out past polls.