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

Frank Potter could.net at gmail.com
Fri Jan 26 06:02:33 EST 2007


Thank you!

On Jan 26, 6:34 pm, Gabriel Genellina <gagsl... at yahoo.com.ar> wrote:
> At Friday 26/1/2007 06:54, Frank Potter wrote:
>
> >[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]
>
> >And, the problem is, it seems that only the last comment is removed.
> >How can I remove all of the comments, please?Note that it's not as easy as simply deleting from // to end of line,
> because those characters might be inside a string literal. But if you
> can afford the risk, this is a simple way without re:
>
> f = open("show_btchina.user.js","r")
> modf = open("modified.js","w")
> for line in f:
>      uline=unicode(line,"utf8")
>      idx = uline.find("//")
>      if idx==0:
>          continue
>      elif idx>0:
>          uline = uline[:idx]+'\n'
>      modf.write(uline.encode("utf8"))
> modf.close()
> f.close()
>
> --
> Gabriel Genellina
> Softlab SRL
>
> __________________________________________________
> Preguntá. Respondé. Descubrí.
> Todo lo que querías saber, y lo que ni imaginabas,
> está en Yahoo! Respuestas (Beta).
> ¡Probalo ya!http://www.yahoo.com.ar/respuestas




More information about the Python-list mailing list