Re: Re: need some regex help to strip out // comments but not http:// urls
| From: | Matijn Woudt | Date: | Wed, 29 May 2013 17:33:13 +0000 |
| Subject: | Re: Re: need some regex help to strip out // comments but not http:// urls | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-321242@lists.php.net to get a copy of this message | ||
On Wed, May 29, 2013 at 7:27 PM, Sean Greenslade <zootboysean@gmail.com>wrote:
> > 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
>
> I would have to disagree. There are things that regex just can't at a
> fundamental level grok. Things like nested brackets (e.g. the standard
> blocking syntax of C, javascript, php, etc.). It's not a parser, and
> despite all the little lookahead/behind tricks that enhanced regex can
> do, it can't at a fundamental level _interret_ the text it sees. This
> task involves interpreting what the text you're looking for actually
> means, and should therefore be handled by something that can
> interpret.
>
I think it should be possible, but as I said, very very complex. Let's not
try it;)
>
> Also, (I haven't tested it, but) I don't think that example you gave
> would work. Without any sort of quoting around the "Kò®2
> \5?Æò—)http://"
> , I would assume the JS interpreter would take that double slash as a
> comment starter. Do tell me if I'm wrong, though.
>
> Which is exactly what I meant. Because http is a var set to 5, it is a
valid case statement, it would be equal to:
switch(value) {
case 5: // Http case here! (this whould not be deleted)
// Do something
}
But any regex given above would treat the first one as a http url, and
won't strip the // and everything after it, though in this modified case it
will strip the comments.
- Matijn