Here is a regex solution. I have provided several test cases which are intended to help you specify your real requirements. (The 'description' field tells what is special about each string.) I have assumed that by 'number', you mean 'unsigned integer'.
use strict;
use warnings;
use Test::Simple tests=>7;
my $NUMBER = qr/[1-9]\d*/; # Unsigned integer
sub solution {
return $_[0] =~ s! ($NUMBER:$NUMBER): !$1//!xr;
}
my @test_cases = (
#given expected description
['3:4:', '3:4//', 'minimal'],
['3:4:5:', '3:4//5:', 'begining of string'],
['foo:fum3:4:', 'foo:fum3:4//', 'end of string'],
['3456:7890:5:', '3456:7890//5:', 'big integers'],
['a:b:', 'a:b:', 'no number'],
['3:4foo:', '3:4foo:', 'misplaced colon'],
['3456:0890:a:', '3456:0890:a:', 'invalid integer'],
);
foreach my $case (@test_cases) {
my ($given, $expected, $description) = @$case;
my $computed = solution($given);
ok( $computed eq $expected, $description);
}
OUTPUT,
1..7
ok 1 - minimal
ok 2 - begining of string
ok 3 - end of string
ok 4 - big integers
ok 5 - no number
ok 6 - misplaced colon
ok 7 - invalid integer
I encourage you to modify this solution and/or list of test cases as your requirements evolve.
|