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