用于检测注释的简单正则表达式(不在字符串内)

时间:2015-08-02 06:51:31

标签: java regex comments

这似乎是一个简单的问题,但我在正则表达式方面遇到了麻烦。我需要一个能够检测//不在引号内的正则表达式。

E.g:
//comment
String ex = "//I do not need to detect this";

1 个答案:

答案 0 :(得分:0)

正则表达式/\/\/((?:(?!\/\/)[^\n])+)$/gm将捕获所有评论。

//comments here
String ex1 = "//I do not need to detect this"; //but I need this
String ex2 = "//I do not need to detect this";
String ex3 = "//I do not need to detect this";// this also

将分别返回comments herebut I need thisthis also

希望这是你想要的!