requestion regarding regular expression

Kelie kelie.feng at gmail.com
Fri Apr 14 05:17:50 EDT 2006


Hello,

I'm trying to analyze some autolisp code with python.  In the file to
be analyzed there are many functions.  Each function begins with a
"defun" statement.  And before that, there may or may not have comment
line(s), which begins with ";".  My goal is to export each function
into separate files, with comments, if there is any.  Below is the code
that I'm struggling with:

[code]

path = "C:\\AutoCAD\\LSP\\Sub.lsp"
string = file(path, 'r').read()

import re
pat = "\\;+.+\\n\\(DEFUN"
p = re.compile(pat,re.I)

iterator = p.finditer(string)
spans = [match.span() for match in iterator]

for i in range(min(15, len(spans))):
    print string[spans[i][0]:spans[i][1]]

[/code]

The code above runs fine.  But it only takes care of the situation in
which there is exactly one comment line above the "defun" statement.
How do I repeat the sub-pattern "\\;+.+\\n" here?
For example if I want to repeat this pattern 0 to 10 times, I know
"\\;+.+\\n{0:10}\\(DEFUN" does not work. But don't know where to put
"{0:10}".  As a work around, I tried to use
pat = "|".join(["\\;+.+\\n"*i+ "\\(DEFUN" for i in range(11)]), and it
turned out to be very slow.  Any help?  

Thank you.

Kelie




More information about the Python-list mailing list