#!/usr/bin/perl use warnings; use strict; foreach ('A000' .. 'B999') { print "$_ "; if (/^(?: A(?:000|12[3-9]|1[3-9]\d|[2-3]\d\d|4[0-4]\d|45[0-6]|999) | B(?:000|789|79\d|8[0-7]\d|88[0-8]) )$/x) { print "match\n"; } else { print "does not match\n"; } }