Hi Folks,
I'm trying to strip C/C++ style comments (/* ... */ or // ) from
source code using Python regexps.
If I don't have to worry about comments embedded in strings, it seems
pretty straightforward (this is what I'm using now):
cpp_pat = re.compile(r
/\* .*? \*/ |# C
#
import re, sys
def q(c):
Returns a regular expression that matches a region delimited by c,
inside which c may be escaped with a backslash
return r%s(\\.|[^%s])*%s % (c, c, c)
single_quoted_string = q('')
Is there some equivalent feature in Python regexps?
cpp_pat = re.compile('(/\*.*?\*/)|(.*?)', re.S)
def subfunc(match):
if match.group(2):
return match.group(2)
else:
return ''
stripped_c_code = cpp_pat.sub(subfunc, c_code)
...I suppose this is what the Perl code might do, but
Is there some equivalent feature in Python regexps?
cpp_pat = re.compile('(/\*.*?\*/)|(.*?)', re.S)
def subfunc(match):
if match.group(2):
return match.group(2)
else:
return ''
stripped_c_code = cpp_pat.sub(subfunc, c_code)
...I suppose this is what the Perl code might do, but
Neat! I didn't realize that re.sub could take a function as an
argument. Thanks.
Lorin
--
http://mail.python.org/mailman/listinfo/python-list