1 2 Previous Next 19 Replies Latest reply: Feb 3, 2009 1:21 PM by 800282 RSS

    Regex on comment /*.....*/ in Java

    807588
      What will regex on comment /*...*/ look like? If I try this:

      [][\*][.]+[\*][]

      It doesn't work. For example, for the string: "/*llsadf3daa*/".
        • 1. Re: Regex on comment /*.....*/ in Java
          807588
          . has no special meaning inside a character class.

          Try this.
          [/][\*].+[\*][/]
          • 2. Re: Regex on comment /*.....*/ in Java
            Darryl Burke
            The block comment is liable to span several lines, so you need the DOTALL flag. Also, the purpose of a character class is defeated if the clss contains only one character. And I think you need a reluctant qualifier so that the regex doesn'tmatch from the first /* to the last */ but matches each block comment separately.
            String regex = "(?s)/\\*.+?\\*/";
            Note: Won't match an empty (but legal) comment block/**/ For doing that, replace the .+? with .*?

            db
            • 3. Re: Regex on comment /*.....*/ in Java
              807588
              You forgot to describe your goal, what you are working on and your context. But as a WAG, have you considered StreamTokenizer?
              • 4. Re: Regex on comment /*.....*/ in Java
                807588
                Thanks for answers. The purpose is that I just want to write a small application that removes all comments from java file. I already wrote it BUT without using regular expressions. And I came to conclusion that with regex it would be more effecient but I don't know so far how to implement this. For example you should consider that such a comment could be in quotes: System.out.println ("/*This is not a comment*/"); and should NOT be removed.
                • 5. Re: Regex on comment /*.....*/ in Java
                  800282
                  codingMonkey wrote:
                  . has no special meaning inside a character class.

                  Try this.
                  [/][\*].+[\*][/]
                  Neither does the * (star).
                  • 6. Re: Regex on comment /*.....*/ in Java
                    800282
                    Exaserge wrote:
                    Thanks for answers. The purpose is that I just want to write a small application that removes all comments from java file. I already wrote it BUT without using regular expressions. And I came to conclusion that with regex it would be more effecient but I don't know so far how to implement this. For example you should consider that such a comment could be in quotes: System.out.println ("/*This is not a comment*/"); and should NOT be removed.
                    Forget regex-es for this. It is definitely NOT the right tool for this job.
                    • 7. Re: Regex on comment /*.....*/ in Java
                      jschellSomeoneStoleMyAlias
                      Exaserge wrote:
                      What will regex on comment /*...*/ look like? If I try this:

                      [][\*][.]+[\*][]

                      It doesn't work. For example, for the string: "/*llsadf3daa*/".
                      And what if the java code looks like the following?
                      System.out.println("/* something */");
                      • 8. Re: Regex on comment /*.....*/ in Java
                        jschellSomeoneStoleMyAlias
                        Exaserge wrote:
                        The purpose is that I just want to write a small application that removes all comments from java file.
                        Why?

                        And I doubt that a correctly functioning application would be deemed 'small'. The only correct way to do this is to implement a java parser. And you could not use a regex to do that.
                        • 9. Re: Regex on comment /*.....*/ in Java
                          800282
                          jschell wrote:
                          Exaserge wrote:
                          What will regex on comment /*...*/ look like? If I try this:

                          [][\*][.]+[\*][]

                          It doesn't work. For example, for the string: "/*llsadf3daa*/".
                          And what if the java code looks like the following?
                          System.out.println("/* something */");
                          Someone might come up with a regex-hack that can account for such String-occurrences by "counting" the number of double quotes using look-ahead, but such a hack will then fail on comments like this:
                          /*
                          a comment with a single double quote in it "
                          */{code}
                          
                          To make a long story short: the OP needs a real parser, not some regex-voodoo (I know you are aware of this, it is more meant as advice towards the OP).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                          • 10. Re: Regex on comment /*.....*/ in Java
                            807588
                            Darryl.Burke wrote:
                            Note: Won't match an empty (but legal) comment block/**/ For doing that, replace the .+? with .*?

                            db
                            It matches :)
                            Enter your regex: (?s)/\\*.+?\\*/
                            Enter input string to search: /**/
                            I found the text "/**/" starting at index 0 and ending at index 4.
                            • 11. Re: Regex on comment /*.....*/ in Java
                              Darryl Burke
                              System.out.println("abc/**/def".replaceAll("(?s)/\\*.+?\\*/", ""));
                              prints
                              abc/**/def
                              db
                              • 12. Re: Regex on comment /*.....*/ in Java
                                807588
                                prometheuzz wrote:
                                To make a long story short: the OP needs a real parser, not some regex-voodoo (I know you are aware of this, it is more meant as advice towards the OP).
                                Well, he needs the lexical analysis front end of a parser, anyhowdy.
                                • 13. Re: Regex on comment /*.....*/ in Java
                                  800282
                                  BigDaddyLoveHandles wrote:
                                  prometheuzz wrote:
                                  To make a long story short: the OP needs a real parser, not some regex-voodoo (I know you are aware of this, it is more meant as advice towards the OP).
                                  Well, he needs the lexical analysis front end of a parser, anyhowdy.
                                  True, but after defining his/her tokens and "lexing" the source file(s), there will need to be written some sort of logic (ignoring comments) which will be his/her parser, right?
                                  • 14. Re: Regex on comment /*.....*/ in Java
                                    807588
                                    prometheuzz wrote:
                                    BigDaddyLoveHandles wrote:
                                    prometheuzz wrote:
                                    To make a long story short: the OP needs a real parser, not some regex-voodoo (I know you are aware of this, it is more meant as advice towards the OP).
                                    Well, he needs the lexical analysis front end of a parser, anyhowdy.
                                    True, but after defining his/her tokens and "lexing" the source file(s), there will need to be written some sort of logic (ignoring comments) which will be his/her parser, right?
                                    The requirements will creep in by themselves. No need to herd them in.
                                    1 2 Previous Next