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

Toby etatoby at gmail.com
Sat Jan 27 11:42:16 EST 2007


Frank Potter wrote:
> r=re.compile(ur"//[^\r\n]+$", re.UNICODE|re.VERBOSE)
> f_new=r.sub(ur"",f)

>From the documentation:

re.MULTILINE
	When specified [...] the pattern character "$" matches at the
	end of the string and at the end of each line (immediately
	preceding each newline). By default [...] "$" matches only at
	the end of the string.

re.DOTALL
	[...] without this flag, "." will match anything except a newline.

So a simple solution to your problem would be:

r = re.compile("//.*")
f_new = r.sub("", f)


Toby



More information about the Python-list mailing list