my %tests = (
'123' => '123',
'abc123def' => '',
'abc 123 def' => '123',
'1 2 4 567' => '567',
'1 2 4 567 890' => '567',
'1 2 3 4567 890' => '890',
);
foreach my $s (keys %tests) {
my ($x) = $s =~ /\b(\d{3})\b/;
$x ||= ''; # set x to empty string if it is falsy
print "Test ($s) did not yield the right outcome ($x)\n"
unless $x eq $tests{$s};
}
print "End of test\n";