how to remove c++ comments from a cpp file?

Paul McGuire ptmcg at austin.rr.com
Fri Jan 26 12:53:04 EST 2007


On Jan 26, 3:54 am, "Frank Potter" <could.... at gmail.com> wrote:
>
> I'm very sorry because I was in a hurry when I post this thread.
> I'll post again my code here:
> [CODE]
> import re
>
> f=open("show_btchina.user.js","r").read()
> f=unicode(f,"utf8")
>
> r=re.compile(ur"//[^\r\n]+$", re.UNICODE|re.VERBOSE)
> f_new=r.sub(ur"",f)
>
> open("modified.js","w").write(f_new.encode("utf8"))
> [/CODE]
>

Here's a pyparsing version that will stay clear of '//' inside quoted
strings. (untested)

-- Paul


from pyparsing import javaStyleComment, dblQuotedString

f=open("show_btchina.user.js","r").read()
f=unicode(f,"utf8")

commentFilter = Suppress( javaStyleComment ).ignore( dblQuotedString )
f_new= commentFilter.transformString(f)

open("modified.js","w").write(f_new.encode("utf8"))




More information about the Python-list mailing list