用于查找*(* 007 *)*的Java Regex不起作用,会喜欢一些建议

时间:2018-04-20 20:13:45

标签: java regex junit

我正在尝试实现一个接受字符串的方法,如果它包含以下内容,则解析为true:"(""后跟007之后的任何内容,后跟&#34后面的任何内容) ;)"其次是任何事情。

换句话说:*(*007*)*

我目前的模式如下:

Pattern.compile("\\*\\s\\(\\s\\*\\s(0\\s0\\s7\\s)\\*\\s\\)\\*");

但是,我的229个junit测试用例中有84个失败了

1 个答案:

答案 0 :(得分:2)

它不会只是像

那样简单
.*\(.*007.*\).*

See it in action

Escaped for Java

.compile(".*\\(.*007.*\\).*")

一些测试用例:

String pattern = ".*\\(.*007.*\\).*";

Assert.assertFalse("(006)".matches(pattern));
Assert.assertFalse("007".matches(pattern));

Assert.assertTrue("hi(this007is)me".matches(pattern));
Assert.assertTrue("hi(007)".matches(pattern));