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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank Potter

    #1

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

    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.V ERBOSE)
    f=file.open("my cpp.cpp","r")
    f=unicode(f,"ut f8")
    r.sub(ur"",f)

    Will somebody show me the right way?
    Thanks~~

  • Gary Herron

    #2
    Re: how to remove c++ comments from a cpp file?

    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.V ERBOSE)
    f=file.open("my cpp.cpp","r")
    f=unicode(f,"ut f8")
    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



    Comment

    • Frank Potter

      #3
      Re: how to remove c++ comments from a cpp file?


      On Jan 26, 5:08 pm, Gary Herron <gher...@island training.comwro te:
      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.V ERBOSE)
      f=file.open("my cpp.cpp","r")
      f=unicode(f,"ut f8")
      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
      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"))
      And, the problem is, it seems that only the last comment is removed.
      How can I remove all of the comments, please?

      Comment

      • Gabriel Genellina

        #4
        Re: how to remove c++ comments from a cpp file?

        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"))
        >
        >
        >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_btch ina.user.js","r ")
        modf = open("modified. js","w")
        for line in f:
        uline=unicode(l ine,"utf8")
        idx = uline.find("//")
        if idx==0:
        continue
        elif idx>0:
        uline = uline[:idx]+'\n'
        modf.write(ulin e.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!


        Comment

        • Frank Potter

          #5
          Re: how to remove c++ comments from a cpp file?

          Thank you!

          On Jan 26, 6:34 pm, Gabriel Genellina <gagsl...@yahoo .com.arwrote:
          Code:
          >[QUOTE]
          f=open("show_btchina.user.js","r").read()
          f=unicode(f,"utf8")
          At Friday 26/1/2007 06:54, Frank Potter wrote: >
          Code:
          import re
          >[QUOTE] r=re.compile(ur"//[^\r\n]+$", re.UNICODE|re.VERBOSE) f_new=r.sub(ur"",f)[/QUOTE] >[QUOTE] open("modified.js","w").write(f_new.encode("utf8")) [/QUOTE]
          >
          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_btch ina.user.js","r ")
          modf = open("modified. js","w")
          for line in f:
          uline=unicode(l ine,"utf8")
          idx = uline.find("//")
          if idx==0:
          continue
          elif idx>0:
          uline = uline[:idx]+'\n'
          modf.write(ulin e.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[/QUOTE]

          Comment

          • Laurent Rahuel

            #6
            Re: how to remove c++ comments from a cpp file?

            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()
            Gabriel Genellina 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"))
            >>
            >>
            >>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_btch ina.user.js","r ")
            modf = open("modified. js","w")
            for line in f:
            uline=unicode(l ine,"utf8")
            idx = uline.find("//")
            if idx==0:
            continue
            elif idx>0:
            uline = uline[:idx]+'\n'
            modf.write(ulin e.encode("utf8" ))
            modf.close()
            f.close()
            >
            >

            Comment

            • Peter Otten

              #7
              Re: how to remove c++ comments from a cpp file?

              Laurent Rahuel wrote:
              And using the codecs module
              Why would you de/encode at all?

              Peter

              Comment

              • Paul McGuire

                #8
                Re: how to remove c++ comments from a cpp file?

                On Jan 26, 3:54 am, "Frank Potter" <could....@gmai l.comwrote:
                >
                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"))
                >
                Here's a pyparsing version that will stay clear of '//' inside quoted
                strings. (untested)

                -- Paul


                from pyparsing import javaStyleCommen t, dblQuotedString

                f=open("show_bt china.user.js", "r").read()
                f=unicode(f,"ut f8")

                commentFilter = Suppress( javaStyleCommen t ).ignore( dblQuotedString )
                f_new= commentFilter.t ransformString( f)

                open("modified. js","w").write( f_new.encode("u tf8"))

                Comment

                • Gabriel Genellina

                  #9
                  Re: how to remove c++ comments from a cpp file?

                  "Peter Otten" <__peter__@web. deescribió en el mensaje
                  news:epd751$518 $03$1@news.t-online.com...
                  Laurent Rahuel wrote:
                  >
                  >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


                  Comment

                  • Toby

                    #10
                    Re: how to remove c++ comments from a cpp file?

                    Frank Potter wrote:
                    r=re.compile(ur "//[^\r\n]+$", re.UNICODE|re.V ERBOSE)
                    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

                    Comment

                    Working...