0

I have simple code look like this

function session(){
   return 1; // this default value for session
}

I need regex or code to remove the comment // this is default value for session, And only remove this type of comment, which starts by a space or two or more, then //, then a newline after it.

All other types of comment and cases are ignored.

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
angry kiwi
  • 10,730
  • 26
  • 115
  • 161
  • 3
    Couldn't you modify the answer from the last question: http://stackoverflow.com/a/34683699/3933332 ? – Rizier123 Jan 09 '16 at 04:30
  • This is distinguished question. This will be good for other to study the process part by part rather than throw in a 200 character regex heaven. – angry kiwi Jan 09 '16 at 04:34

2 Answers2

1

UPDATED (1)

And only remove this type of comment, which starts by a space or two or more, then //, then a newline after it

Try this one:

regex101 1

PHP Fiddle 1 -hit "run" or F9 to see the result

/\s+\/\/[^\n]+/m
  • \s+ starts by a space or two or more
  • \/\/ the escaped //
  • [^\n]+ anything except a new line

UPDATE: to make sure -kinda-this only applied to code lines, we can make use of the lookbehind (2) regex to check if there is a semicolon ; before the space[s] and the comment slashes //, so the regex will be this:

regex101 2

PHP Fiddle 2

/(?<=;)\s+\/\/[^\n]+/m

where (?<=;) is the lookbehind which basically tells the engine to look behind and check if there's a ; before it then match.


-----------------------------------------------------------------------

(1) The preg_replace works globally, no need for the g flag

(2) The lookbehind is not supported in javascript

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
  • error preg_replace(): Unknown modifier 'g'. I run it like this $html = preg_replace('/\s+\/\/[^\n]+/gm', '', $html); – angry kiwi Jan 09 '16 at 06:41
  • @angry_kiwi, my fault, `preg_replace()` just works globally this why it doesn't need the `g` flag, check this http://phpfiddle.org/lite/code/ts8s-fywa -*hit run or F9 to execute and see the result*-, I apologize I'll update my answer – Mi-Creativity Jan 09 '16 at 07:01
  • @angry_kiwi, you may consider using the regex with the lookbehind way http://phpfiddle.org/lite/code/q4cw-kuv7 – Mi-Creativity Jan 09 '16 at 07:11
0

A purely regex solution would look something like this:

$result = preg_replace('#^(.*?)\s+//.*$#m', '\1', $source);

but that would still be wrong because you could get trapped by something like this:

$str = "This is a string // that has a comment inside";

A more robust solution would be to completely rewrite the php code using token_get_all() to actually parse the PHP code into tokens that you can then selectively remove when you re-emit the code:

foreach(token_get_all($source) as $token)
{
    if(is_array($token))
    {
        if($token[0] != T_COMMENT || substr($token[1] != '//', 0, 3))
            echo $token[1];
    }
    else
        echo $token;
}
jbafford
  • 5,528
  • 1
  • 24
  • 37