in reply to Re: removing C style comments from text files
in thread removing C style comments from text files

I address this problem by replacing quoted strings with unique tags prior to removing the comments. After stripping the comments I then restore the original strings for those tags remaining (some may get stripped).

  • Comment on Re: Re: removing C style comments from text files

Replies are listed 'Best First'.
Re: removing C style comments from text files
by Abigail-II (Bishop) on Jan 06, 2004 at 13:16 UTC
    That's kind of a chicken-and-egg problem, isn't? How can you succesfully remove strings, if you can't detect comments? Consider:
    /* One " two */ a = b + 4; /* three " four */
    You have to do it all in one pass. Something like:
    s { ( [^"'/]* # Not a string, character o +r comment. | "[^\\"]*(?:\\.[^\\"]*)*" # String. | '[^\\']*(?:\\.[^\\']*)*' # Char. | / (?![*]) # Slash, not a comment. ) | ( /[*] [^*]* (?: [*] [^*/]* )* [*]/ ) # Comment. } { $2 ? "" : $1 }gsex;
    But that isn't fool proof either (consider # define).

    Abigail