Version 1 of HOW TO - Remove C style comments

Updated 2005-09-03 17:01:31

The following procedure will remove C style comments (i.e. /* .... */ ) from text.

 proc removeComments { text {replacement ""} } {
    regsub -all {[/][*].*?[*][/]} $text ${replacement} text
    return $text        
 }

If you need to remove C style comments that are imbedded (i.e. /* ... /* ... */ ... */) use the following procedure.

 proc removeImbeddedComments { text {replacement ""} } {
     set text [string map  {"/*" \x80 "*/" \x81} $text]
     while {[regsub -all {\x80[^\x80\x81]*?\x81} $text ${replacement} text]} {continue}
     set text [string map  {\x80 "/*" \x81 "*/"} $text]
     return $text
 }

Use Examples:

 removeComments ${data} "#comment-removed#"

 removeImbeddedComments ${data} "#comment-removed#"

Tom Krehbiel