c:\@Work\Perl>perl -wMstrict -le "my $master = 'comp.hw.new.'; ;; my @items = qw( comp.hw. comp.hw.new. comp.hw.hw. comp.sw.old. muse.hw.new. ancient. ); ;; LEVEL: for my $level (1 .. 4) { print qq{Match Level: $level}; ;; my ($buf) = $master =~ m{ \A (?: [^.]* [.]){$level} }xmsg or print qq{no master match at level $level}; next LEVEL unless defined $buf; print qq{ Master: '$buf'}; ;; for my $str (@items) { if ($str =~ m{ \A (?i) \Q$buf\E }xms) { print qq{ '$str' matches '$buf'}; } else { print qq{ '$str' DOES NOT match '$buf'}; } } } " Match Level: 1 Master: 'comp.' 'comp.hw.' matches 'comp.' 'comp.hw.new.' matches 'comp.' 'comp.hw.hw.' matches 'comp.' 'comp.sw.old.' matches 'comp.' 'muse.hw.new.' DOES NOT match 'comp.' 'ancient.' DOES NOT match 'comp.' Match Level: 2 Master: 'comp.hw.' 'comp.hw.' matches 'comp.hw.' 'comp.hw.new.' matches 'comp.hw.' 'comp.hw.hw.' matches 'comp.hw.' 'comp.sw.old.' DOES NOT match 'comp.hw.' 'muse.hw.new.' DOES NOT match 'comp.hw.' 'ancient.' DOES NOT match 'comp.hw.' Match Level: 3 Master: 'comp.hw.new.' 'comp.hw.' DOES NOT match 'comp.hw.new.' 'comp.hw.new.' matches 'comp.hw.new.' 'comp.hw.hw.' DOES NOT match 'comp.hw.new.' 'comp.sw.old.' DOES NOT match 'comp.hw.new.' 'muse.hw.new.' DOES NOT match 'comp.hw.new.' 'ancient.' DOES NOT match 'comp.hw.new.' Match Level: 4 no master match at level 4