Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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.


Comment on Re: need regex help to strip things like embedded C comments
Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-10-02 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (52 votes), past polls