phizel has asked for the wisdom of the Perl Monks concerning the following question:

Does a profiler exist for regular expressions? The only thing I could find was re's debug mode, but the output isn't very intuitive. I am imagining something like Devel::NYTProf with flame graphs indicating which sections of a regex need optimizing. Take the common whitespace trimming example s/^\s+|\s+$//g; it turns out to sub-optimal because the alternation negates the anchor optimization. Since I can't make heads or tails of re's debug mode output, the only tool in my toolbox for diagnosing regexen is to compare runtimes with Benchmark. Since regular expressions are a feature of many other languages, a better solution might even exist outside of perl.

Replies are listed 'Best First'.
Re: Regex profiler
by Corion (Patriarch) on Jan 18, 2026 at 07:25 UTC
Re: Regex profiler
by choroba (Cardinal) on Jan 17, 2026 at 20:16 UTC
    > a better solution might even exist outside of perl.

    Note that other languages might have similar regex syntax to Perl, but the implementation is definitely different, so profiling/benchmarking it doesn't really help if you're going to use the regex in Perl.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Regex profiler
by LanX (Saint) on Jan 19, 2026 at 13:06 UTC
    Depending on what you mean by "profiling" you can inject Perl (?{code}) snippets into your regex to record the current time. (IMHO that's the most likely approach a third party profiler would take)

    But please be aware that you might get a Heisenbug situation then, because your snippet was hindering some of the many internal heuristics or optimizations.

    Looking into re debug mode before and after those injections is always a good approach.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery