简体   繁体   中英

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

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*(\/\/|#).*

Demo

Description:

The single-line comments can start at the beginning of the line or after a 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 doesn't care for the characters after it, the comment with //~ will also be matched.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM