#! perl -slw use strict; my $re = qr[^\d{3}-\d{3}$]; my $pat = '111-111'; my @inputs = ( '', 'a', '1', '12', '12x', '123', '1234', '123-', '123x', '123-4', '123-x', '123-456', '123-4567' ); for my $input ( @inputs ) { print "$input failed" and next if length $input > length $pat; my $test = $input . substr( $pat, length( $input ) ); print $input, $test =~ $re ? ' passed' : ' failed'; } __END__ P:\test>471541 passed a failed 1 passed 12 passed 12x failed 123 passed 1234 failed 123- passed 123x failed 123-4 passed 123-x failed 123-456 passed 123-4567 failed #### #! perl -slw use strict; my @re = ( '^$', '^\d{0,3}$', '^\d{3}-$', '^\d{3}-\d{0,3}$', '^\d{3}-\d{3}$', ); my $re = join '|', reverse @re; $re = qr[$re]; my @inputs = ( '', 'a', '1', '12', '12x', '123', '1234', '123-', '123x', '123-4', '123-x', '123-456', '123-4567' ); for my $input ( @inputs ) { print $input, $input =~ $re ? ' passed ' : ' failed ', 'the alternation'; for my $reg ( @re ) { if( $input =~ $reg ) { print "$input passed"; last; } else { print "$input failed"; } } } __END__ P:\test>471541-2 passed the alternation passed a failed the alternation a failed a failed a failed a failed a failed 1 passed the alternation 1 failed 1 passed 12 passed the alternation 12 failed 12 passed 12x failed the alternation 12x failed 12x failed 12x failed 12x failed 12x failed 123 passed the alternation 123 failed 123 passed 1234 failed the alternation 1234 failed 1234 failed 1234 failed 1234 failed 1234 failed 123- passed the alternation 123- failed 123- failed 123- passed 123x failed the alternation 123x failed 123x failed 123x failed 123x failed 123x failed 123-4 passed the alternation 123-4 failed 123-4 failed 123-4 failed 123-4 passed 123-x failed the alternation 123-x failed 123-x failed 123-x failed 123-x failed 123-x failed 123-456 passed the alternation 123-456 failed 123-456 failed 123-456 failed 123-456 passed 123-4567 failed the alternation 123-4567 failed 123-4567 failed 123-4567 failed 123-4567 failed 123-4567 failed