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

Gary Herron gherron at islandtraining.com
Fri Jan 26 04:08:57 EST 2007


Frank Potter wrote:
> I only want to remove the comments which begin with "//".
> I did like this, but it doesn't work.
>
> r=re.compile(ur"//[^\r\n]+$", re.UNICODE|re.VERBOSE)
> f=file.open("mycpp.cpp","r")
> f=unicode(f,"utf8")
> r.sub(ur"",f)
>
> Will somebody show me the right way?
> Thanks~~
>
>   
If you expect help with a problem, it would be nice if you told us what 
the problem is. What error did you get?

But even without that I see lots of errors:

You must import re before you use it:
import re

Open a file with open((..) not file.open(...).

Once you open the file you must *read* the contents and operate on that:
data = f.read()

Then you ought to close the file:
f.close()

Now you can do your sub on the string in data -- but note, THIS WON'T 
CHANGE data, but rather returns a new string which you must assign to 
something:

new_data = r.sub(ur"", data)

Then do something with the new string.

Also I fear your regular expression is incorrect.

Cheers,
Gary Herron






More information about the Python-list mailing list