in reply to How can I expand my substring?
use strict; use warnings; my $maxLen = 20; my @tests = ( ['N' . ('-' x ($maxLen + 20)) . 'B', 'N' . ('-' x 20) . ('B' x ($m +axLen + 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" }
which generates no output indicating all is well.
The two regex sustitutions:
$str =~ s/((?=N-+)(?:N)(?:-*?))(-{1,$maxLen}B)/$1 . ('B' x length +$2)/eg; $str =~ s/(B-{1,$maxLen})(?=-*(N|$))/'B' x length $1/eg;
do the work. The rest is test code. Change maxLen to the maximum number of characters to replace.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How can I expand my substring?
by Anonymous Monk on Jul 23, 2014 at 14:39 UTC | |
by GrandFather (Saint) on Jul 23, 2014 at 20:58 UTC |