|
|
| Perl Monk, Perl Meditation | |
| PerlMonks |
Re: Regex to strip comments (match strings)by tye (Cardinal) |
| on Oct 01, 2012 at 04:31 UTC ( #996579=note: print w/ replies, xml ) | Need Help?? |
|
You just have to teach your regex to match things that might contain '/*' characters that don't represent comments. This mostly boils down to string literals. Though, if there is a chance of "// end-of-line" comments, then you have to match those as well. My code above strips them too. (Updates made shortly after posting below:) If you want to be defensive against mistakes in your regex or in your understanding of the syntax you are trying to parse, then you can add \G(?: and ) around the regex in order to prevent the possibility of it just skipping over unhandled stuff. You can then also specifically match "end of string" for similar reasons. I think the "(.)" case is simple enough that I have little worry of getting that part of the regex wrong and it serves the "misunderstood syntax" and "don't skip bits, including at end of string" purposes well enough. - tye
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||