【发布时间】:2016-01-05 07:09:04
【问题描述】:
如何使用正则表达式对检测进行评论,但如果在字符串中,它应该不起作用。
例如:
//----------------------example-----------------------------------------
class fo{
void foo(){
/***print comment
*/
System.out.println("example writing comment // this is comment");
System.out.println("example comment 1 /* comment1 */");
System.out.println("example comment 2 /* comment2 "+
"*/");
}
}
这是我的评论检测模式:
Pattern.compile("^([^\"]|\"[^\"]*\")*?((/\\*([^\\*]|(\\*(?!/))+)*+\\*+/)|(//.*))");
但它不起作用
所以,// this is comment, /* comment1 */, /* comment2 "+
"*/" 不能匹配。
【问题讨论】:
标签: java regex string comments