Regular expression to remove comments from code

Wondering how to delete all comments in java or xml, are you still looking for shortcut keys in eclipse, you are out, now you are using regular expressions,

First of all, we understand that the annotations in java are nothing more than 3:

1. // single line comment    

2 /* */ Multi-line comment

3 /** */ Documentation comments

We can use the replacement method in Ctrl+F to solve the problem, and replace all the comments with empty, which solves the purpose of deleting comments. Here we need to use regular expressions to select Regular expression in options. Fill in the regular expression in the box above

Remove  java  comments /* */: /\*{1,2}[\s\S]*?\*/

Remove java comments //: //[\s\S]*?\n

Remove xml comments: <!-[\s\S]*?-->

Remove blank lines: ^\s*\n

\n or \r\n or \r may be newlines, depending on whether your system is windows or Unix/Linux or mac

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326233065&siteId=291194637