Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: need regex help to strip things like embedded C comments

by jimt (Chaplain)
on Jul 21, 2007 at 22:47 UTC ( #628044=note: print w/replies, xml ) Need Help??

in reply to need regex help to strip things like embedded C comments

printf("remember, /* this isn't a comment */"); printf("/* This isn't a comment %s */", /* but this is */ "/* this isn +'t, though */); // /* The code between these lines int x = y + z; // is not commented out. The line level comments take precedent */

In short, this is a potentially nasty problem. I would strongly recommend you read the very excellent Mastering Regular Expressions from O'Reilly by Jeffrey Friedl.

Regardless, any solution you come up with using regexes will probably only operate on a carefully crafted subset of the data, so proceed with caution. There are also variations depending upon which implementation of C you're using.

Replies are listed 'Best First'.
Re^2: need regex help to strip things like embedded C comments
by Eradicatore (Monk) on Jul 21, 2007 at 23:29 UTC
    Thanks for the reply! Yes, I agree there are definitely going to be gradations of how well any solution works here. I dont' need anything perfect. But I'm also mainly wondering about my one specific question. How to get somethinig like character negation but have it be more like "pattern negation". In this case, the pattern I don't want to be inside the non-greedy regex is /* which is the opening to a new c comment.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://628044]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2017-09-23 07:43 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (272 votes). Check out past polls.