Regular expression to remove comment

后端 未结 5 2065
轮回少年
轮回少年 2021-02-07 14:49

I am trying to write a regular expression which finds all the comments in text. For example all between /* */. Example:

/* Hello */

5条回答
  •  Happy的楠姐
    2021-02-07 15:15

    Unlike the example posted above, you were trying to match comments that spanned multiple lines. By default, . does not match a line break. Thus you have to enable multi-line mode in the regex to match multi-line comments.

    Also, you probably need to use .*? instead of .*. Otherwise it will make the largest match possible, which will be everything between the first open comment and the last close comment.

    I don't know how to enable multi-line matching mode in Sublime Text 2. I'm not sure it is available as a mode. However, you can insert a line break into the actual pattern by using CTRL + Enter. So, I would suggest this alternative:

    /\*(.|\n)*?\*/
    

    If Sublime Text 2 doesn't recognize the \n, you could alternatively use CTRL + Enter to insert a line break in the pattern, in place of \n.

提交回复
热议问题