Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trace the execution of a regex matching?

Is there a convenient way to get a trace of the regex engine's states during the evaluation of a m// or s/// expression?


BTW, I'm aware of that there is a Regexp::Debugger package available through CPAN (and it is amazingly cool), but I don't see a way to get anything like a trace from it; I don't want to step through a potentially huge number of steps.

like image 335
kjo Avatar asked Jan 29 '23 17:01

kjo


1 Answers

Yes. Turn the regex engine into debug mode and it'll print what it's doing:

use re 'debug'; 

my $str = "abcdefg";
$str =~ m/[ef]+/;

Which gives an output of:

Compiling REx "[ef]+"
Final program:
   1: PLUS (13)
   2:   ANYOF[ef] (0)
  13: END (0)
stclass ANYOF[ef] plus minlen 1 
Matching REx "[ef]+" against "abcdefg"
Matching stclass ANYOF[ef] against "abcdefg" (7 bytes)
   4 <abcd> <efg>            |  1:PLUS(13)
                                  ANYOF[ef] can match 2 times out of 2147483647...
   6 <abcdef> <g>            | 13:  END(0)
Match successful!
Freeing REx: "[ef]+"
like image 164
Sobrique Avatar answered Feb 01 '23 22:02

Sobrique