use strict; use warnings; my $maxLen = 20; my @tests = ( ['N' . ('-' x ($maxLen + 20)) . 'B', 'N' . ('-' x 20) . ('B' x ($maxLen + 1))], ['-B' . ('-' x ($maxLen + 20)), '-B' . ('B' x $maxLen) . ('-' x 20)], ['-N-N--B--N', '-N-NBBBBBN'], ['N-N-B-', 'N-NBBB'], ['-B-N-B-N-B-N', '-BBNBBBNBBBN'], ['-B-N', '-BBN'], ['-B-', '-BB'], ['B-', 'BB'], ); for my $test (@tests) { my ($org, $ref) = @$test; my $str = $org; $str =~ s/((?=N-+)(?:N)(?:-*?))(-{1,$maxLen}B)/$1 . ('B' x length $2)/eg; $str =~ s/(B-{1,$maxLen})(?=-*(N|$))/'B' x length $1/eg; next if $str eq $ref; print "Couldn't handle: '$org'\n"; print " Expected: '$ref'\n"; print " Got: '$str'\n" }