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

Toby etatoby at
Sat Jan 27 17:42:16 CET 2007

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

>From the documentation:

	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.

	[...] 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)


More information about the Python-list mailing list