Look for all matches, select the longest one, and then perform your replacement. Something like (untested):
my $string = '00110011111110111111111111111110'; my $str_to_replace; for ($string=~/(1+)/g) { my $temp = $1; $str_to_replace = $temp if length($tmp) > length($str_to_replace); } if ($str_to_replace) { my $repl = '2' x length($str_to_replace); $string =~ s/$str_to_replace/$repl/; }
...roboticus
When your only tool is a hammer, all problems look like your thumb.
In reply to Re^3: how to replace a matched pattern in a string with a different pattern of same length?
by roboticus
in thread how to replace a matched pattern in a string with a different pattern of same length?
by BhariD
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |