Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What tools are there for debugging/stepping through a regular expression?

While there are many good online and offline tools for testing regular expressions, I have yet to find a tool (besides RegexBuddy) that lets me see the steps that the regular expression engine takes to find a match (or to find that there is no match, for that matter.)

Does anyone know of any tools that do this? The reason I have initially excluded RegexBuddy is because it's commercial and I would prefer to look over my options before I decide to pay for it.

To make it clearer what I want, here is a screenshot from RegexBuddy (from their home page): http://img166.imageshack.us/img166/1272/debug.png

like image 345
Blixt Avatar asked Jul 16 '09 13:07

Blixt


4 Answers

In Perl you can always just use re 'debug'; or use re 'debugcolor';

For example if you enter this into Perl:

use strict;
use warnings;
use 5.010;
use re 'debug';

# using the same strings as the question's image for reference:

my $str = 'Even if I do say so myself: "RegexBuddy is awesome"';
$str =~ /(Regexp?Buddy is (awful|acceptable|awesome))/;

This is what you get out:

Compiling REx "(Regexp?Buddy is (awful|acceptable|awesome))"
Final program:
   1: OPEN1 (3)
   3:   EXACT <Regex> (6)
   6:   CURLY {0,1} (10)
   8:     EXACT <p> (0)
  10:   EXACT <Buddy is > (14)
  14:   OPEN2 (16)
  16:     EXACT <a> (18)
  18:     TRIEC-EXACT[cw] (29) # this is a new feature in Perl 5.10
          <wful> 
          <cceptable> 
          <wesome> 
  29:   CLOSE2 (31)
  31: CLOSE1 (33)
  33: END (0)

anchored "Regex" at 0 floating "Buddy is a" at 5..6 (checking floating) minlen 19 
Guessing start of match in sv for REx "(Regexp?Buddy is (awful|acceptable|awesome))" against 'Even if I do say so myself: "RegexBuddy is awesome"'
Found floating substr "Buddy is a" at offset 34...
Found anchored substr "Regex" at offset 29...
Starting position does not contradict # /^/m...
Guessed: match at offset 29

Matching REx "(Regexp?Buddy is (awful|acceptable|awesome))" against 'RegexBuddy is awesome"'
  29 <'lf: "'> <RegexBuddy>  |  1:OPEN1(3)
  29 <'lf: "'> <RegexBuddy>  |  3:EXACT <Regex>(6)
  34 <Regex> <Buddy is a>    |  6:CURLY {0,1}(10)
                                  EXACT <p> can match 0 times out of 1...
  34 <Regex> <Buddy is a>    | 10:  EXACT <Buddy is >(14)
  43 <y is > <'awesome"'>    | 14:  OPEN2(16)
  43 <y is > <'awesome"'>    | 16:  EXACT <a>(18)
  44 < is a> <'wesome"'>     | 18:  TRIEC-EXACT[cw](29)
  44 < is a> <'wesome"'>     |      State:    2 Accepted:    0 Charid:  2 CP:  77 After State:    3
  45 < is aw> <'esome"'>     |      State:    3 Accepted:    0 Charid:  7 CP:  65 After State:   10
  46 < is awe> <'some"'>     |      State:   10 Accepted:    0 Charid:  b CP:  73 After State:   11
  47 < is awes> <'ome"'>     |      State:   11 Accepted:    0 Charid:  c CP:  6f After State:   12
  48 < is aweso> <'me"'>     |      State:   12 Accepted:    0 Charid:  d CP:  6d After State:   13
  49 < is awesom> <'e"'>     |      State:   13 Accepted:    0 Charid:  7 CP:  65 After State:   14
  50 < is awesome> <'"'>     |      State:   14 Accepted:    1 Charid:  3 CP:   0 After State:    0
                                    got 1 possible matches
                                    only one match left: #3 <wesome>
  50 < is awesome> <'"'>     | 29:  CLOSE2(31)
  50 < is awesome> <'"'>     | 31:  CLOSE1(33)
  50 < is awesome> <'"'>     | 33:  END(0)
Match successful!
Freeing REx: "(Regexp?Buddy is (awful|acceptable|awesome))"

( Note: I changed some parts of the output, so that it would highlight better )

like image 164
Brad Gilbert Avatar answered Nov 05 '22 18:11

Brad Gilbert


It's not quite as powerful as RegexBuddy, but it's a simple online interface: http://www.gskinner.com/RegExr/

You can mouse-over parts of your expression and it will tell you what it's doing. Very basic, but it can really save time when you do something stupid.

like image 38
Dan Breen Avatar answered Nov 05 '22 18:11

Dan Breen


Perhaps RegexCoach offers what you're looking for...

http://weitz.de/regex-coach/

like image 1
groovingandi Avatar answered Nov 05 '22 18:11

groovingandi


Debuggex has the best graphical approach I've seen out there. It basically shows you what a DFA diagram would look like for the given regular expression, has basic highlighting for the match, and has a slider at the bottom that lets you step through the match and DFA diagram step-by-step:

enter image description here

like image 1
Alex W Avatar answered Nov 05 '22 18:11

Alex W