Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

RE: Multi Line c comments

by kilinrax (Deacon)
on Nov 15, 2000 at 23:28 UTC ( [id://41838]=note: print w/replies, xml ) Need Help??


in reply to Multi Line c comments

Update: This code, as pointed out below, will not work if the text in the comment contains any asterisks.

See How do I use a regular expression to strip C style comments from a file? for a regexp that will work in all cases.


I am pretty sure that this would work:
$text =~ s|\/\*[^\*]*\*\/||;
Breaking that down a bit (i can appreciate how it might just look like a bunch of asterisks and slashes, but i guess that's probably a good thing as far as your obfuscation goes ;-):
$text =~ s| \/\* # '/*', escaped [^\*]* # 0 or more non-'*' characters \*\/ # '*/', escaped ||x;

Replies are listed 'Best First'.
RE: RE: Multi Line c comments
by $code or die (Deacon) on Nov 15, 2000 at 23:48 UTC
    Hmm. Not sure about this. I seem to remember that getting rid of C comments was a pain - Death to Dot Star. Specifically Merlin's Post and Ovid's Responses to that thread.

    I know there is no dot-star in that regex, and it uses negative character classes but I am worried that it might fall into the same trap as the ones in the above nodes. I suppose, I could try it to see.

    e.g. is this a valid C comment:
    /* This my comment It spans a couple* of lines. * actually it spans 5 lines but it has 3 *s in it */

    It seems to me that the above regex won't work here. Maybe it's not valid C comment.

    Actually, I don't know what I'm talking about - I have been stuck at work today for too long - I just needed to node. I am going for a break.
RE (tilly) 2: Multi Line c comments
by tilly (Archbishop) on Nov 15, 2000 at 23:54 UTC
    I am a little scared, considering that this is a FAQ, how many people are getting it wrong.

    This has a very common failure mode that looks like this:

    /***************************************** * Script Foo * * Written by "I like pretty comments" * * The purpose of this is... * * etc/and more... * ******************************************/
    This fairly generic example (and common variations) will defeat most simplistic attempts at this problem. :-)

    (See the FAQ for more.)

      You are correct, my regexp was naive and wrong; and not remembering something in the faq is pretty inexcusable :-(

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://41838]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-03-28 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found