Perhaps old news to some, but a search didn't show this anywhere in the Monastery yet. So... Found a really interesting tool on CPAN to analyse perl regex matching: Regexp::Debugger.

You run it in a terminal and it interactively steps through the regex showing you each stage of the pattern matching. More importantly it shows you where a lot of back tracking is happening which can be the reason for slow execution.

Without images it's tough to appreciate how cool and useful this can be. If you haven't seen this before grab a copy and run something like this from your terminal:

perl -MRegexp::Debugger -e '"abcdef" =~ /(a|b) b+ c/'

And then just press space bar to step through the matching process. But there are many more features than that, press '?' to see all the keyboard options. That is a rather pedestrian example, it gets much more exciting when you have embedded code execution and lots of subgroups.

To top it off it has a REPL mode that allows you to type in a string and a regex on the fly:

perl -MRegexp::Debugger -E 'Regexp::Debugger::rxrx(@ARGV)'

Where you can then enter a regex with '/', and a string with single-quote, and then start the process with 'm'. Press q to return to the start screen to adjust your regex or string... rinse and repeat.


In reply to Regex analysis by Loops

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.