RegEx to remove / ** * / and // ** ** // php comments

ReGex is new here.

I need to remove two different style comments from PHP files using RegEx.

I found this expression to run in the BBEdit text editor:

\/\*[\s\S]*?\*\/

and removes comments in the first style, as shown below:

/** This comment gets removed with my regex */

But he does not remove these style comments:

// ** This comment has the double leadng-trailng slashes ** //

I don’t know why there is a combination of two different types of comments, and there are only a few comments //, but I need to delete all of them.

Adding another slash to the search, i.e.

\/\\*[\s\S]*?\*\/

makes the expression greedy and removes individual slashes in uncommented code. A working expression will require much more complexity :)

+4
source share
3 answers
~//?\s*\*[\s\S]*?\*\s*//?~

. \ .

. .

http://regex101.com/r/lK9zP6/6

+1

PHP :

php_strip_whitespace($filename);
+5

You can use this regex:

\/\*.*?\*\/|\/\/.*?\n

Working demo

enter image description here

Like the Hwnd pointed out in this answer, if you change the delimiter to , you can use a cleared regular expression: ~

/\*.*?\*/|//.*?\n
+2
source

All Articles