[email protected] (Ralph Corderoy) Sat, 07 Oct 2006 11:07:29 +0100
Newsgroups perl.perlfaq.workers
Message-ID <[email protected]>
Hi,

[I'm not subscribed, so please CC me.]

http://www.ayni.com/perldoc/perl5.8.0/pod/perlfaq6.html#How-do-I-use-a-regular-expression-to-strip-C-style-comments-from-a-file-
ends with "A slight modification also removes C++ comments".  The
suggested regexp doesn't work since C++ comments can be spread over more
than one line by escaping the end of the line with a backslash.

    $ cat x.c++
    int a; // abc\
    int b; // ghi
    $ g++ -E x.c++
    # 1 "x.c++"
    # 1 "<built-in>"
    # 1 "<command line>"
    # 1 "x.c++"
    int a;
    $ cat strip
    while (<>) {
        s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#$2#gs;
        print;
    }
    $ perl strip x.c++
    int a;
    int b;
    $

Either the regexp needs fixing, or that bit of the answer should be
removed.

Cheers,


Ralph.