Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

bikeNomad already answered your question, just let me elaborate a little bit on some of your statements

  • (Honestly, I don't know why; this was a suggestion from a more experienced engineer, ...)
    you should always try to see the logic behind such statements. In your case of matching different pattern against a string, this solution might be more effective (to shorten my code, consider $string to be in $_): if (/($m1)/ || /($m2)/ || /($m3)/) This is quicker then your big regex if you know that $m1 will be more frequent than $m2 (which in turn is more frequent than $m3).
    The explanation is simple, in your case the regex engine has to check all three patterns for every position in the string. With the || construct, the checking of the $m2 and $m3 pattern might not be necessary. So you see, it pays off, to know your data. (Remark: In general, the size of a regex says nothing about its execution time, only the compile time increases. Execution time depends on the content!)
  • If speed is an issue, consider the /o modifier and qr//. Furthermore you might try to lowercase the whole string and the patterns instead of matching /i. And studying the string might also help.
  • For all this advice, the 'correct' way can be found out by using Benchmark on some real data. By doing that you can find out yourself what works when and don't have to rely on some 'experts'.
  • The FAQs/resources don't offer any aid, and other Perl wizards say it can't be done.
    Just to show you TMTOWTDI - not that I recommend this if (/($m1(?{$match=1})|$m2(?{$match=2})|$m3(?{$match=3}))/) { ... you can execute code from within the regex ... and this is a really bad example of this powerful feature - but a way to do it :)

-- Hofmator

In reply to Re: Returning _which_ pattern matched...? by Hofmator
in thread Returning _which_ pattern matched...? by Boudicca

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2023-06-08 17:32 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (35 votes). Check out past polls.