replace c-style comments with newlines (regexp)
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Thu Dec 20 19:34:58 EST 2007
On Fri, 21 Dec 2007 00:00:47 +0000, lex __ wrote:
> I'm tryin to use regexp to replace multi-line c-style comments (like /*
> this /n */ ) with /n (newlines). I tried someting like
> re.sub('/\*(.*)/\*' , '/n' , file) but it doesn't work for multiple
> lines.
Regexes won't cross line boundaries unless you make them multiline with
re.MULTILINE.
Also, I'm no expert on regexes, but it looks to me that your regex is
greedy. I think you need the non-greedy version, which by memory (and
completely untested) is something like this:
rx = re.compile('/\*(.*?)/\*', re.MULTILINE)
Have you considered what happens when your C code includes a string
literal containing '/*'?
"Some people, when confronted with a problem, think “I know, I’ll use
regular expressions.” Now they have two problems."
-- Jamie Zawinski, in comp.lang.emacs
--
Steven.
More information about the Python-list
mailing list