#! perl -slw use strict; my $input = 'A000,A123-A456,A999-B000,B789-B888'; my $set = join'', map{ m[ ( [A-Z] \d{3} ) (?:- ( [A-Z] \d{3} ) )?]x; $1 .. $2||$1 } split ',', $input; for my $testVal ( 'A100', 'A234', 'B000', 'B789 does match', 'B788 but not this' ){ ## The test if( $testVal =~ m[([A-Z]\d{3})] and 1+index( $set, $1 ) ) { print "'$testVal' contains a match for '$input'"; } else { print "No match for '$testVal'"; } } __END__ P:\test>362578 No match for 'A100' 'A234' contains a match for 'A000,A123-A456,A999-B000,B789-B888' 'B000' contains a match for 'A000,A123-A456,A999-B000,B789-B888' 'B789 does match' contains a match for 'A000,A123-A456,A999-B000,B789-B888' No match for 'B788 but not this'