0

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!

4
  • 1
    Have you considered using a proper tokenizer instead of a regex? You will be able to throw out only the comment tokens. Commented Dec 13, 2010 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 */ Commented Dec 13, 2010 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! Commented Dec 13, 2010 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? Commented Dec 13, 2010 at 21:12

1 Answer 1

1

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.

2
  • # 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! Commented Dec 13, 2010 at 21:09
  • @Sam, you're welcome. Weekend? That will take a few more days, but thanks and you have a nice day too! :) Commented Dec 14, 2010 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.