#!/usr/bin/env perl use strict; use warnings; use constant { STR => 0, EXP => 1, }; use Test::More; my @tests = ( ['1-2-3', '123'], ['-1-2-3', '-123'], ['1--2-3', ''], ['1-2--3', ''], ['1--2--3', ''], ['-1--2-3', ''], ['-1-2--3', ''], ['-1--2--3', ''], ['1-2-', ''], ['-1-2-', ''], ['garbage', ''], ['expect', 'failure'], ['-2-3-4', '-234'], ); plan tests => 0+@tests; my $re = qr{(?x: ^ # start of string ( # start capture X -? # optional leading minus \d+ # 1 or more digits ) # end capture X - # required hyphen ( # start capture Y \d+ # 1 or more digits ) # end capture Y - # required hyphen ( # start capture Z \d+ # 1 or more digits ) # end capture Z $ # end of string )}; for my $test (@tests) { my ($X, $Y, $Z, $got) = ('') x 4; if (($X, $Y, $Z) = $test->[STR] =~ $re) { $got = "$X$Y$Z"; } ok($got eq $test->[EXP], "Testing '$test->[STR]' is " . (length $test->[EXP] ? 'GOOD' : 'BAD') ); } #### 1..13 ok 1 - Testing '1-2-3' is GOOD ok 2 - Testing '-1-2-3' is GOOD ok 3 - Testing '1--2-3' is BAD ok 4 - Testing '1-2--3' is BAD ok 5 - Testing '1--2--3' is BAD ok 6 - Testing '-1--2-3' is BAD ok 7 - Testing '-1-2--3' is BAD ok 8 - Testing '-1--2--3' is BAD ok 9 - Testing '1-2-' is BAD ok 10 - Testing '-1-2-' is BAD ok 11 - Testing 'garbage' is BAD not ok 12 - Testing 'expect' is GOOD # Failed test 'Testing 'expect' is GOOD' # at ./pm_11148100_re_parse.pl line 55. ok 13 - Testing '-2-3-4' is GOOD # Looks like you failed 1 test of 13.