Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 在缺少@Ignore注释时匹配Checkstyle的正则表达式_Regex_Checkstyle - Fatal编程技术网

Regex 在缺少@Ignore注释时匹配Checkstyle的正则表达式

Regex 在缺少@Ignore注释时匹配Checkstyle的正则表达式,regex,checkstyle,Regex,Checkstyle,我正在尝试创建一个自定义的Checkstyle规则,当开发人员在没有注释的情况下使用@Ignore时,该规则将标记为错误。因此,我正在寻找一个符合以下场景的正则表达式: @Ignore @Test public void someTest() { ... } @Ignore("some comment detailing why this test was ignored") @Test public void someTest() { ... } 还有这个: @Ignore @

我正在尝试创建一个自定义的Checkstyle规则,当开发人员在没有注释的情况下使用
@Ignore
时,该规则将标记为错误。因此,我正在寻找一个符合以下场景的正则表达式:

@Ignore
@Test
public void someTest() {
   ...
}
@Ignore("some comment detailing why this test was ignored")
@Test
public void someTest() {
   ...
}
还有这个:

@Ignore @Test    //or @Test @Ignore
public void someTest() {
   ...
}
@Test
public void someTest() {
   ...
}
但不符合这种情况:

@Ignore
@Test
public void someTest() {
   ...
}
@Ignore("some comment detailing why this test was ignored")
@Test
public void someTest() {
   ...
}
或者这个:

@Ignore @Test    //or @Test @Ignore
public void someTest() {
   ...
}
@Test
public void someTest() {
   ...
}
因此,基本上它是一个正则表达式,匹配
@Ignore
,但仅当它存在时,并且仅当它存在时没有限定的注释,例如
@Ignore(“comment here”)

匹配
@Ignore
,仅当它后面没有左括号时

说明:

@Ignore   # Match "@Ignore"
(?!       # Assert that we can't match...
 [ \t]*   # optional spaces/tabs
 \(       # followed by a ( at the current position
)         # End of lookahead
在Java中:

Pattern regex = Pattern.compile("@Ignore(?![ \\t]*\\()");

你可以试试这个:

@Ignore\s*$
实际上,对于junit测试,注释:

@Test @Ignore
public void testXXXX(){}
同样有效。所以这也需要匹配

已更新

这应该可以:

@Ignore\s*(?!\()

您使用的是哪个正则表达式引擎?解决方案可能会有所不同,这取决于@Tim(如中所述),我假设它是javatim。看起来这将适用于我上面的原始示例。然而,正如肯特所指出的,我忘记了另一个场景(现在我的问题又加了一个)@Chris:好的,我已经重写了我的答案。这对你有用吗?具体来说,空格/制表符或其他空格是否可能出现在
@Ignore
和注释之间?是的,空格可能出现在
@Ignore
和注释之间。例如,
@Ignore(“一些评论”)
有效(例如不应该匹配),谢谢Kent。我喜欢你的简单性,只是当你有“忽略测试”的时候它不起作用。我猜你在做一些代码检查。也许有一个IDE?IDE通常具有代码格式化程序/检查样式功能。可以定义在注释后添加新行。(每行一条注释)这不仅是为了解决这个问题,还可以使代码更具可读性。感谢Kent,完全同意,我们开始使用自动代码格式,但是我们有很多旧代码,它们的格式不一致。对不起,希望我也能接受你的回答!