java匹配注释的正则表达式

参考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|.)*?\\*\\/", "")
}



posted @ 2012-01-25 12:58  雪霁霜飞  阅读(16948)  评论(2编辑  收藏  举报