I actually gave a lightening talk on "How not to abuse regular expressions" at YAPC::Europe 2000. A couple of months later japhy posted Code Smarter which also touches on some of the ideas I mentioned. I had a list of common regexp idioms that are far more efficient when recast as substr, index and so on, I should see if I still have my notes lying around.

There are also a couple of paradoxical things to be aware of with regexps. It may seem that s/^\s+|\s+$/g should be faster than s/^\s+//; s/\s+$//. But it's nearly always not. The reason is that in the former case, the string has to be scanned in its entirety. In the latter case, only the beginning and end of the string need to be examined. The longer the string, the bigger the win.


print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

In reply to Re: Seduced by a Regex by grinder
in thread Seduced by a Regex by kelan

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.