How do I remove multiline comments from php files with regex? I tried "/\*(.*\n)*\*/" but it fails. All it does is starts with "/*" and stops at the last occurrence of "*/".
Asked
Active
Viewed 2,034 times
4
-
2[Documentation](http://php.net/manual/en/reference.pcre.pattern.modifiers.php). `/\*(.+?)\*/s` – tenub Apr 02 '14 at 18:20
-
1Regex might not be able to deal with all edge cases. Also, see [this question](http://stackoverflow.com/q/503871/1578604). – Jerry Apr 02 '14 at 18:33
-
/\\*(.+?)\\*/ works if comment is on single line – tfE Apr 02 '14 at 18:38
1 Answers
4
Thanks to the tenub comment.
The final solution for all php comments looks like this:
/\*[\s\S]*?\*/|(?<!http:)//.*
Where
1) /\*[\s\S]*?\*/
for /*comments*/
2) (?<!http:)//.*
for single line //comment
escaping urls starting with http://
(it would be better to prevent the ones preceded by "
or '
from showing but I'm good for now)
Oh and tenub if you post a corrected answer instead of the comment. I'll accept it. Cos you helped ty)

tfE
- 170
- 5
- 12
-
1A more generic regex would be `/\*[\s\S]*?\*/|//.*` because `[\s\S]` will be able to match every character. – Jerry Apr 02 '14 at 20:37
-
Thank you Jerry. Well I really should learn regex sometime. It's unbelievably useful. – tfE Apr 02 '14 at 21:05
-
+1 for `[\s\S]`, which seems to be the only solution in PhpStorm to match all `.` inclusively new line characters (searched pretty long to find that). – Matteo B. Mar 30 '15 at 10:56