The easiest way to detect if something matches two patterns is to just try both pattern matches, for example:

if ($line =~ m/balls/ and $line =~ m/rings/) { # ... }

This can be done with one regex, too, but it's much more complicated (and probably slower) and more difficult to maintain, for example:

m/balls.*?rings|rings.*?balls/s

update: A better technique to combine the two regexes into one is presented in chipmunk's reply to this node. It's not only more maintainable, but it's probably faster then my regex here, at least.

update 2: I have benchmarked my combined regex, chipmunk's regex, and just using two regexes. (All tuned for matching "rings" and "balls" of course.) Using two regexes is signigantly faster (by 30% for the string "ringsballs" with that factor getting much larger the longer the searched string is.) My combined regex tends to be the slowest of them all, and chipmunk's comes in second. (though performance was close to my combined regex when the string being searched started with one of the strings "rings" or "balls" and there was a lot of junk text in the string being matched.)

(The benchmark was performed on strings composed of a number of random characters, followed by "rings" followed by a number of random characters followed by "balls" followed by a number of random characters, and the same without the first set of random characters. Random characters were all selected from lowercase letters a through z.)


In reply to Re: Multiple matching using m/// by wog
in thread Multiple matching using m/// by DaWolf

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.