Regexp::Debug?

miyagawa on 2007-10-12T19:15:54

Lazyweb,

Is there a module to debug your regular expression, to compare the target string and an input regular expression one byte by one? It'd be useful if you have an existent code to do a pattern match against a big chunk of string and don't know why it doesn't match.

use Regexp::Debug;

my $string = "abcdefg"; my $regexp = qr/abcefg/; # Notice 'd' is missing

my $result = Regexp::Debug->compare($string, $regexp);

# $result would be an object or a string to # indicate that the regexp stopped matching at 'abc'


It's the thing we regularly do, when the regular expression based screen scraping tool (BAD! Use Web::Scraper instead!) stops working. I open up 2 terminal screens, one with HTML output and one with regular expression. In the worst case I split the regular expression in a binary tree search fashion to find where it's broken.


re.pm

jjore on 2007-10-12T20:19:57

use re 'debug'.

In later perls it is lexically scoped.

Re:re.pm

miyagawa on 2007-10-12T21:02:56

Oh, yes. Perl built-in pragma. /me bangs my head to the desk.

Hmm, but the output of use re 'debug' doesn't seem intuitive if it DOESN'T match.

Re:re.pm

agent on 2007-10-24T10:20:54

Man, are you looking something like this in Perl 5?

http://agentzh.org/misc/tracer/langs/index.html

It works for unsuccessful matches as well :)

It's a tracer for Perl 6 regexes though ;) It's a tool provided by PCR. Hopefully Perl 5 can have something analogous to this.