7 Aug
2003
7 Aug
'03
8:13 p.m.
sre_dump.py -- http://www.dalkescientific.com/Python/ Converts an sre_parse parse tree back into a regular expression, and includes information about where subexpressions can be found in that string. I have a certain fondness for writing regular expression based parsers. /F's sre_parse module (an internal part of the standard library) has been very fun to use in these projects. It turns a regular expression into a simple parse tree, which I use to generate my own parsers. When debugging, I want to know which part of the regular expression contributed to the part of the parser with the bug. The sre_dump module helps by using the parse tree to recreate the original pattern and tracking where the branches are in the string. The result is that I can do >>> s, offsets = sre_dump.dump_and_offsets("AB|CD") >>> def show_offsets(s, offsets): ... print s ... for expr, i, j, text in offsets: ... print " "*i + "-"*(j-i) + " " *(len(s)-j+1), s[i:j] ... >>> show_offsets(s, offsets) AB|CD - A - B - C - D ----- AB|CD >>> Andrew dalke@dalkescientific.com
7603
Age (days ago)
7603
Last active (days ago)
0 comments
1 participants
participants (1)
-
Andrew Dalke