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.

2
  • 3
    Couldn't you modify the answer from the last question: stackoverflow.com/a/34683699/3933332 ? Commented Jan 9, 2016 at 4: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. Commented Jan 9, 2016 at 4:34

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

error preg_replace(): Unknown modifier 'g'. I run it like this $html = preg_replace('/\s+\/\/[^\n]+/gm', '', $html);
@angry_kiwi, my fault, preg_replace() just works globally this why it doesn't need the g flag, check this phpfiddle.org/lite/code/ts8s-fywa -hit run or F9 to execute and see the result-, I apologize I'll update my answer
@angry_kiwi, you may consider using the regex with the lookbehind way phpfiddle.org/lite/code/q4cw-kuv7
The m pattern modifier is useless if you don't have any start of line or end of line metacharacters (^ or $).
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;
}

Comments

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.