in reply to removing C style comments from text files

use Regexp::Common; $string =~ s/$RE{comment}{C}//g;

Note that this (just like the regexes presented in the rest of this thread) isn't context aware, and happily removes "comments" from strings.

Abigail

Replies are listed 'Best First'.
Re: Re: removing C style comments from text files
by EdwardG (Vicar) on Jan 06, 2004 at 12:49 UTC

    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).

      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