如何在任何编辑器中通过Regex删除所有单行PHP注释行 [英] How to delete all single-line PHP comment lines through Regex in any editor
问题描述
我在Geany / Notepad ++之类的编辑器中打开了一个php文件,该文件同时具有单行注释和块注释两种类型。
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 ?
推荐答案
正则表达式 ^ [#] [\ /] {2} [〜]。*
是匹配从#//〜
。
正则表达式与
^#\/\/~.*
使用正则表达式
Use the regex
^\s*(\/\/|#).*
描述:
单行注释可以在行的开头或几个空格(缩进)之后开始。
The single-line comments can start at the beginning of the line or after few spaces(indentation).
-
^
:行的开头 -
\s *
:任意空格数 -
(\ / \ / |#)
:匹配//
或#
个字符。|
在正则表达式中为OR。 -
。*
:匹配任意字符(
^
: 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
请注意, PHP注释在 // 之后不包含波浪号
〜
code>。即使〜
在 //
之后出现,因为上述正则表达式检查 //
并且不在乎后面的字符,带有 //〜
的注释也将匹配。
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.
这篇关于如何在任何编辑器中通过Regex删除所有单行PHP注释行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!