问题
I have a php file open in editor like Geany/Notepad++ which has both type of comments single-line and block-comments.
Now as block-comments are useful for documentation, I only want to remove single-line comments starting with //~
or #
. Other comments starting with //
should remain if they are not starting line from //
.
How can I do that in regex, I tried this one below but get stuck up in escaping slash and also including #
.
^[#][\/]{2}[~].*
Anyone can help ?
回答1:
The problem with the regex ^[#][\/]{2}[~].*
is that it match line starting with #//~
.
The regex is same as
^#\/\/~.*
Use the regex
^\s*(\/\/|#).*
Demo
Description:
The single-line comments can start at the beginning of the line or after few spaces(indentation).
^
: Start of the line\s*
: Any number of spaces(\/\/|#)
: Match//
or#
characters.|
is OR in regex..*
: Match any characters(except newline) any number of times
Note that PHP comments does not contain tilde ~
after //
. Even if ~
is present after //
as the above regex checks for //
and don't care for the characters after it, the comment with //~
will also be matched.
来源:https://stackoverflow.com/questions/39825607/how-to-delete-all-single-line-php-comment-lines-through-regex-in-any-editor