Michael J. Fromberger wrote:
It's not the most elegant solution in the world. This code does NOT deal with the problem of commented text. I think it will handle triple quotes, though I haven't tested it on that case.
I believe that it will probably work for triple quotes that begin and end on the same line. Of course, the primary usage of triple-quotes is for multiline strings, but given that the file is being examined one line at a time, you'd need some method of maintaining state in order to handle multiline strings properly. (Note that this problem is true regardless of whether the strings are true triple-quoted multiline strings, or single-quoted single-line strings broken across two lines of source code using '\'.)
If the entire file is read in and processed as a single chunk, instead of line-by-line, then *some* of the problems go away (at the cost of potentially very large memory consumption and poor performance, if the file is large). The fact that triple-quoted strings work out (mostly) correctly when viewed as three pairs of quotes will help. But if a triple-quoted string *contains* a normally quoted string (e.g., """My "foo" object"""), then things break down again.
In order to handle this sort of nested structure with anything resembling true reliability, it's necessary to step up to a true lexing/parsing procedure, instead of mere string matching and regular expressions.
Jeff Shannon Technician/Programmer Credit International
-- http://mail.python.org/mailman/listinfo/python-list