requestion regarding regular expression

Kent Johnson kent at kentsjohnson.com
Fri Apr 14 08:36:47 EDT 2006


Kelie wrote:
> 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.

ISTM you don't need regex here, a simple line processor will work. 
Something like this (untested):

path = "C:\\AutoCAD\\LSP\\Sub.lsp"
lines = open(path).readlines()

# Find the starts of all the functions
starts = [i for i, line in enumerate(lines) if line.startswith('(DEFUN')]

# Check for leading comments
for i, start in starts:
   while start > 0 and lines[start-1].startswith(';'):
     starts[i] = start = start-1

# Now starts should be a list of line numbers for the start of each function

Kent



More information about the Python-list mailing list