How to delete all single-line PHP comment lines through Regex in any editor

*爱你&永不变心* 提交于 2019-12-25 07:12:38

问题


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

  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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!