Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

im trying to remove unnessary comments with preg-replace in controlled script situations, but my regex is incorrect. Anyone any ideas whats wrong with my regex? (i have Apache/2.0.54 & PHP/5.2.9

BEFORE:

// Bla Bli Blue Blow Bell Billy Bow Bye
script var etc ();    // cangaroo cognac codified cilly celine cocktail couplet
script http://blaa.org    // you get the idea!

AFTER:

script var etc ();
script http://blaa.org

PROBLEM: what regex to use?

# when comment starts on a new line, delete this entire line
# find [a new line] [//] [space or no space] [comment]
$buffer = preg_replace('??', '??', $buffer);

# when comment is halfway in script (    //  comment)
# find [not beginning of a line] [1 TAB] [//] [1 space again] [comment]
$buffer = preg_replace('??', '??', $buffer);

Any and All suggestions will be valued +1 by me, cuase im so darn close to solve this riddle!

share|improve this question
1  
Have you considered using a proper tokenizer instead of a regex? You will be able to throw out only the comment tokens. –  cdhowie Dec 13 '10 at 18:42
1  
What about multi-line comments? Especially nested multi-line comments? That's hideously non-trivial for regex, but trivial for a tokenizer /* this is a /* nested comment */ –  Marc B Dec 13 '10 at 18:46
    
Writing comments consistently, means there is no room for such a strange thing Marc B, a comment within a commment. Nevertheless ironic that such a lecture on consistency comes from the mouth of a very incapable programmer, namely myself hahah! –  Sam Dec 13 '10 at 21:04
    
Thanks CDHowie, what is a tokenizer? can you give an example how to use that? I have a general question about this regex thing: does it belong to PHP, to Apache or somethin else? and is a Tokenizer same as Regex but with only different syntax? –  Sam Dec 13 '10 at 21:12

1 Answer 1

up vote 1 down vote accepted

Try this regex:

/(?<!http:)\/\/[^\r\n]*/

Be cautious though, consider strings like:

<!-- 
// not a comment -->

or

/* 
// not a comment */

and

var s = "also // not // a // comment";

And you might want to work around https://... and ftp://... etc.

share|improve this answer
    
# BEAUTIFULL!!!! lacking those strange situations with comments in a comment gives space for even cleaner code: your regex worked first time out of the box! wish you a great weekend Bart! –  Sam Dec 13 '10 at 21:09
    
@Sam, you're welcome. Weekend? That will take a few more days, but thanks and you have a nice day too! :) –  Bart Kiers Dec 14 '10 at 7:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.