如何在任何编辑器中通过Regex删除所有单行PHP注释行 [英] How to delete all single-line PHP comment lines through Regex in any editor

查看:116
本文介绍了如何在任何编辑器中通过Regex删除所有单行PHP注释行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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).


  1. ^ :行的开头

  2. \s * :任意空格数

  3. (\ / \ / |#):匹配 // 个字符。 | 在正则表达式中为OR。

  4. 。* :匹配任意字符(

  1. ^: Start of the line
  2. \s*: Any number of spaces
  3. (\/\/|#): Match // or # characters. | is OR in regex.
  4. .*: 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆