I have a list of patterns which I am trying to match against a piece of text. What I want to find is the pattern earliest in the list which matches earliest in the text, where matching earlier in the text is more important than being higher up the list. Perl will do this for me if I do something like /foo|bar|baz/, however this leaves me with a dilemma. I need to know which of the patterns matched, because the action I take next depends on which pattern was successful.

One way I could do this would be to use a pattern similar to the above and then take the text that matched and check it against all of my patterns in order until I find the one it matched. However, this means I am trying to match the same pattern against the same text multiple times, which doesn't really seem like it'd be a good idea. I've also thought about looping through each pattern in turn but I think that would be even worse than the first way. I am wondering if there is a better way to accomplish this? If there isn't I guess I'll just do the match using the alternation but I'd hate to be redundantly re-testing patterns when I don't need to. I'll potentially have quite a lot of patterns to test and quite a lot of text, and I need to find as many matches as possible, so it's worthwhile doing things efficiently. :)

In reply to Finding out which of a list of patterns matched by lemnisca

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.