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.

Comment on Regex analysis
Select or Download Code
Re: Regex analysis
by sundialsvc4 (Monsignor) on Aug 07, 2013 at 23:38 UTC

    Thank you for a good write-up of a very interesting find!   XP++ ...   (And who-cares if it is “old news” to someone-else:   it is timely and useful to The Center of My Personal Programming Universe™ ;-) and betcha I’m not alone.)

Re: Regex analysis
by davido (Archbishop) on Aug 08, 2013 at 00:12 UTC
Re: Regex analysis
by kcott (Abbot) on Aug 08, 2013 at 05:44 UTC

    G'day Loops,

    Actually, that's one of my favourites so ++ and Front-paged for possibly a little more exposure.

    When you install Regexp::Debugger, you also get the rxrx command-line utility which allows you to do (amongst other things):

    $ rxrx script.pl

    See COMMAND-LINE DEBUGGING for details of this and other uses.

    -- Ken

Re: Regex analysis (rxrx bugs)
by Anonymous Monk on Aug 08, 2013 at 09:12 UTC

    if you enter an invalid regex, one that doesn't compile, there is no error message showed, you just can't match it

    if you enter 'string'; the ; breaks it, talk about finnicky :)

    you can only describe from within match

    a bit scroll-flickery on win32