I was searching HTML looking for items that could be separated by, for example, an unknown amount of tags. The code returns two values to show how good the match was. The items to search for are pushed into @names. Ideally $success should be 100 i.e. all elements were matched and $goodness should be 100 i.e. the length of the elements was equal to the length of the matched portion of the string because there were few unwanted characters
$matched = 0; $pmin = -1; $pmax = 0; foreach $namepart (@names) { if (/$namepart/gc) { $matched++; $pmin = pos($_) - length($namepart) if ($pmin == -1); $pmax = pos($_); } } $success = int($matched/($#names+1) * 100); $goodness = int(($pmax - $pmin) / $len * 100); $goodness = 0 if (!$success);

In reply to Inexact Matcher by Odud

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.