Re: Re: need some regex help to strip out // comments but not http:// urls

From: Date: Wed, 29 May 2013 17:20:49 +0000
Subject: Re: Re: need some regex help to strip out // comments but not http:// urls
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-321239@lists.php.net to get a copy of this message
On Wed, May 29, 2013 at 6:08 PM, Sean Greenslade <zootboysean@gmail.com>wrote: > On Wed, May 29, 2013 at 9:57 AM, Jonesy <gmane@jonz.net> wrote: > > On Tue, 28 May 2013 14:17:06 -0700, Daevid Vincent wrote: > >> I'm adding some minification to our cache.class.php and am running into > an > >> edge case that is causing me grief. > >> > >> I want to remove all comments of the // variety, HOWEVER I don't want to > >> remove URLs... > > > > KISS. > > > > To make it simple, straight-forward, and understandable next year when I > > have to re-read what I've written: > > > > I'd change all "://" to "QqQ" -- or any unlikely text string. > > > > Then I'd do whatever needs to be done to the "//" occurances. > > > > Finally, I'd change all "QqQ" back to "://". > > > > Jonesy > > Wow. This is just a spectacularly bad suggestion. > > First off, this task is probably a bit beyond the capabilities of a > regex. Yes, you may be able to come up with something that works 99% > of the time, but this is really a job for a parser of some sort. I'm > sorry I don't have any suggestions on exactly where to go with that, > however I'm sure Google can be of assistance. The main problem is that > regex doesn't understand context. It just blindly finds patterns. A > parser understands context, and can figure out which //'s are comments > and which are something else. As a bonus, it can probably understand > other forms of comments like /* */, which regex would completely die > on. > > It is possible to write a whole parser as a single regex, being it terribly long and complex. That said, there's no other simple syntax that would work, for example in javascript you could to the following: var http = 5; switch(value) { case http:// Http case here! (this whould not be deleted) // Do something } But most likely you wouldn't care about that.. - Matijn

« previous php.general (#321239) next »