參考http://iregex.org/blog/uncomment-program-with-regex.html
通用注釋有兩種:
1、//
2、/*......*/
通常情況下,行級注釋可以這樣匹配
\/\/[^\n]*
塊級別這樣
\/\*([^\*^\/]*|[\*^\/*]*|[^\**\/]*)*\*\/
或者還可以這樣
\/\*(\s|.)*?\*\/
不過在特殊情況中,行級別會跟協議前綴沖突,所以還需要特殊處理
(?<!http:)\/\/.*
甚至於不限定於http協議
(?<!:)\/\/.*
最終處理注釋為:
/**
* 處理注釋 groovy代碼
* @param text
* @return
*/
def removeComment(text) {
return text.replaceAll("(?<!:)\\/\\/.*|\\/\\*(\\s|.)*?\\*\\/", "")
}