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 with a regular expression? I tried this one below, but I get stuck up in escaping slash and also including #
.
^[#][\/]{2}[~].*
The problem with the regex ^[#][\/]{2}[~].*
is that it matches a line starting with #//~
.
The regex is the same as
^#\/\/~.*
Use the regex
^\s*(\/\/|#).*
Description:
The single-line comments can start at the beginning of the line or after a 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 timesNote that PHP comments does not contain tilde ~
after //
. Even if ~
is present after //
, as the above regex checks for //
and doesn't care for the characters after it, the comment with //~
will also be matched.