20
votes

Is there a way to debug a regular expression in Python? And I'm not referring to the process of trying and trying till they work :)

Here is how regexes can be debugged in Perl:


use re 'debug';

my $str = "GET http://some-site.com HTTP/1.1";
if($str =~/get\s+(\S+)/i) {
    print "MATCH:$1\n";
}

The code above produces the following output on my computer when ran:


Compiling REx "get\s+(\S+)"
Final program:
   1: EXACTF  (3)
   3: PLUS (5)
   4:   SPACE (0)
   5: OPEN1 (7)
   7:   PLUS (9)
   8:     NSPACE (0)
   9: CLOSE1 (11)
  11: END (0)
stclass EXACTF  minlen 5
Matching REx "get\s+(\S+)" against "GET http://some-site.com HTTP/1.1"
Matching stclass EXACTF  against "GET http://some-site.com HTTP/1.1" (33 chars)
   0           |  1:EXACTF (3)
   3        |  3:PLUS(5)
                                  SPACE can match 1 times out of 2147483647...
   4       |  5:  OPEN1(7)
   4       |  7:  PLUS(9)
                                    NSPACE can match 20 times out of 2147483647...
  24       |  9:    CLOSE1(11)
  24       | 11:    END(0)
Match successful!
MATCH:http://some-site.com
Freeing REx: "get\s+(\S+)"

7
Seriously: give SO your regex + sample data? - annakata
Without sample code, all we can do is guess what your problem is. - S.Lott
I don't have a regex problem right now. I only want to know how to perform the debugging. - Geo
Maybe what the comments above imply is "To debug python regular expressions, post your SO to stackoverflow.com so poor souls who've gone before you can help" - qneill
Perl has more powerful tool Regexp::Debugger - youtu.be/zcSFIUiMgAs?t=158 - Alexey Shrub

7 Answers

19
votes

>>> p = re.compile('.*', re.DEBUG)
max_repeat 0 65535
  any None
>>>                         

regex '|' operator vs separate runs for each sub-expression

3
votes

https://www.debuggex.com is also pretty good. It's an online Python (and a couple more languages) debugger, which has a pretty neat visualization of what does and what doesn't match. A pretty good resource if you need to draft a regexp quickly.

1
votes

Why don't you use some regEx tool (i usually use Regulator) and test the regex-expression there and when you are satisfied, just copy/paste it into your code.

0
votes

Not sure about doing such a thing directly in Python, but I could definitely suggest using a RegEx editor tool. That's likely to be your best bet anyway. Personally, I've used The Regulator and found it to very helpful. Some others are listed in this SO thread.

0
votes

Similar to the already mentioned, there is also Regexbuddy

0
votes

I quite often use RegexPal for quick checks (an online regular expression prototyper). It has a lot of the common expressions listed along with a simple expression. Very handy when you don't have a dedicated tool and just need a quick way to work out a somple regex.

0
votes

What RegexBuddy has that the other tools don't have is a built-in debugger that shows you the entire matching process of both successful and failed match attempts. The other tools only show the final result (which RegexBuddy can show too).