Tapatalk

What is the regular expression for /* several lines comments */

What is the regular expression for /* several lines comments */

2
NewbieNewbie
2

    Mar 04, 2010#1

    For example:
    /* comment1
    comment2
    comment3 */


    I want delete the comments (including /*and*/)
    One way is using macro. How to do this using just replace command?
    How to represent it in regular expression ?
    I tried ^(/*^)^(*^)^(*/^) or ^(\/\*^)^(*^)^(\*\/^) or ^(^/^*^)^(*^)^(^*^/^), all cannot work.
    I also tried in VIM: %s/\(\/\*\)\(*\)\(\*\/\)//, also doesn't work, I know * limits us to only one line search,
    so any solution to describe the contents that have several lines?

    Thanks a lot

    236
    MasterMaster
    236

      Mar 04, 2010#2

      As a Perl regex, when nested comments are not allowed (/* This /* comment is */ nested */):

      (?s)/\*.*?\*/

      If you want to allow one level of nesting:

      (?s)/\*(?:(?!\*/|/\*).)*+(?:/\*(?:(?!\*/|/\*).)*+\*/(?:(?!\*/|/\*).)*+)*+.*?\*/

      If you want to allow two levels of nesting, you'd better give up.

      And no, I didn't come up with the second regex here, it's from RegexBuddy's library.

      30
      Basic UserBasic User
      30

        Mar 04, 2010#3

        Note that UltraEdit does *not* support RegEx fully. UltraEdit cripples RegEx by applying it on a line by line basis, whereas RegEx is supposed to apply to the entire document for true multi-line support.

        I love UltraEdit, but when I need more advanced RegEx support, I use JGSoft's EditPad Pro. I wish UltraEdit would support RegEx fully, but despite numerous complaints and requests, it has still never happened. So, until they do, I'm stuck using two text editors.

        236
        MasterMaster
        236

          Mar 04, 2010#4

          That is true; I could have written the same thing, word for word. The regex should still work in UE, though :)

          2
          NewbieNewbie
          2

            Mar 05, 2010#5

            Great, thanks to both of you :P

            26
            Basic UserBasic User
            26

              Jun 23, 2010#6

              This perl style regular expression will match a comment block from the first "/" in "/*" to the last "/" in "*/":

              Code: Select all

              ((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))
              I am using UEStudio v10 and it finds/highlights comment blocks when this perl regex is used, it even handles the case where there are multiple '*' after the first "/" and/or multiple '*' before the last "/".

              hth