One possible regex-based approach:

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

Updates:

  1. Oops... Added  \Q...\E to original  m{ \A (?i) $buf }xms because synthesized pattern contains literal  '.' (period) characters. Output unchanged. (quotemeta-ing would be a good idea in any event.)
  2. graff has observed below that the written specs in ozboomer's OP are just a tad bit vague, and I have to agree. What I did was to assume the output of the code (however rubbishy) given in the OP was correct and, with my understanding of the specs, write code giving essentially the same output.


In reply to Re: I think regex Should Help Here... but How!? by AnomalousMonk
in thread I think regex Should Help Here... but How!? by ozboomer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.