Spooferman
Spooferman

Reputation: 353

Perl one liner to remove comments, removes lines which are not comments also

I am using a perl one liner to remove the comments from from a C file, but it also removes the lines which have an asterik(*) in between words, for eg

static void *

Here in the one liner I am using:

'perl -0777 -pe ''s{/\*.*?\*/}{}gs'' ' sourceFile ' > ' destFile

Could anyone please suggest?

Thanks

Upvotes: 0

Views: 663

Answers (3)

user2291758
user2291758

Reputation: 763

Your regex works for me. I just corrected your quoting, I don't know if your are using it like that, or it was a copy&paste artefact.

perl -0777 -pe 's{/\*.*?\*/}{}gs' sourceFile.c >destFile.c

(I am using bash on linux.)

Try it on Ideone.

Upvotes: 0

Doggerel
Doggerel

Reputation: 87

This question is answered in the perlfaq.

"How do I use a regular expression to strip C style comments from a file?"

http://perldoc.perl.org/5.8.9/perlfaq6.html#How-do-I-use-a-regular-expression-to-strip-C-style-comments-from-a-file?

Upvotes: 2

Scott Weaver
Scott Weaver

Reputation: 7361

maybe something like this:

s{/\*((?!\*/).)*\*/}gs

"Look for /*, look ahead to not see the */ sequence and eat one character, repeat the look-and-eat up until */"

Be aware, however, that since the comments can be nested or the /* */ enclosed in quotation marks, you can never reliably parse them out with regular expressions.

Upvotes: 0

Related Questions