Discussion:
how to remove c++ comments from a cpp file?
Paul McGuire
2007-01-26 17:53:04 UTC
Permalink
I'm very sorry because I was in a hurry when I post this thread.
[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"))
Frank Potter
2007-01-26 09:54:05 UTC
Permalink
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?
import re
Open a file with open((..) not file.open(...).
data = f.read()
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
new_data = r.sub(ur"", data)
Then do something with the new string.
Also I fear your regular expression is incorrect.
Cheers,
Gary Herron
Thank you.
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]

And, the problem is, it seems that only the last comment is removed.
How can I remove all of the comments, please?
Frank Potter
2007-01-26 11:02:33 UTC
Permalink
Thank you!
[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
f = open("show_btchina.user.js","r")
modf = open("modified.js","w")
uline=unicode(line,"utf8")
idx = uline.find("//")
continue
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
Toby
2007-01-27 16:42:16 UTC
Permalink
r=re.compile(ur"//[^\r\n]+$", re.UNICODE|re.VERBOSE)
f_new=r.sub(ur"",f)
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
Peter Otten
2007-01-26 15:36:26 UTC
Permalink
And using the codecs module
Why would you de/encode at all?

Peter
Gabriel Genellina
2007-01-26 18:56:17 UTC
Permalink
"Peter Otten" <__peter__ at web.de> escribi? en el mensaje
Post by Peter Otten
And using the codecs module
Why would you de/encode at all?
I'd say the otherwise: why not? This is the recommended practice: decode
inputs as soon as possible, work on Unicode, encode only when you write the
output.
In this particular case, it's not necesary and you get the same results,
only because these two conditions are met:

- the encoding used is utf-8
- we're looking for '//', and no unicode character contains '/' in its
representation using that encoding apart from '/' itself

Looking for the byte sequence '//' into data encoded with a different
encoding (like utf-16 or ucs-2) could give false positives. And looking for
other things (like '??') on utf-8 could give false positives too.
The same applies if one wants to skip string literals looking for '"' and
'\\"'.
Anyway for a toy script like this, perhaps it does not make any sense at
all - but one should be aware of the potential problems.
--
Gabriel Genellina
Laurent Rahuel
2007-01-26 15:23:39 UTC
Permalink
And using the codecs module

[CODE]
import codecs

f = codecs.open("show_btchina.user.js","r","utf-8")
modf = codecs.open("modified.js","w","utf-8")
for line in f:
idx = line.find(u"//")
if idx==0:
continue
elif idx>0:
line = line[:idx]+u'\n'
modf.write(line)
modf.close()
f.close()
[/CODE]
[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
f = open("show_btchina.user.js","r")
modf = open("modified.js","w")
uline=unicode(line,"utf8")
idx = uline.find("//")
continue
uline = uline[:idx]+'\n'
modf.write(uline.encode("utf8"))
modf.close()
f.close()
Gabriel Genellina
2007-01-26 10:34:52 UTC
Permalink
[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
Frank Potter
2007-01-26 08:46:37 UTC
Permalink
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~~
Gary Herron
2007-01-26 09:08:57 UTC
Permalink
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
Continue reading on narkive:
Loading...