Debugging RE

Oct 6, 2014

One of Python's most useful feature is its Regular Expression support. However, it is easy to get a regular expression wrong and if you do, debugging it can be a painful as well. Fortunately, Python allows us to set a DEBUG flag during compilation of a regular expression.

import re
pat = re.compile('^search/(0x)+([0-9A-Za-z]*)/([0-9]{0,3}/)?$', re.DEBUG)

Then we get the following output. This shows us the patters and sub patterns by using proper python like indentation and also describes the content of each pattern/sub pattern along with number of repetitions allowed.

at at_beginning
literal 115
literal 101
literal 97
literal 114
literal 99
literal 104
literal 47
max_repeat 1 4294967295
    subpattern 1
        literal 48
        literal 120
subpattern 2
    max_repeat 0 4294967295
        in
            range (48, 57)
            range (65, 90)
            range (97, 122)
literal 47
max_repeat 0 1
    subpattern 3
        max_repeat 0 3
            in
                range (48, 57)
        literal 47
at at_end