Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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
用于查找*(*007*)*的Java正则表达式不起作用,希望得到一些建议_Java_Regex_Junit - Fatal编程技术网

用于查找*(*007*)*的Java正则表达式不起作用,希望得到一些建议

用于查找*(*007*)*的Java正则表达式不起作用,希望得到一些建议,java,regex,junit,Java,Regex,Junit,我试图实现一个方法来接受一个字符串,如果它包含:anything后跟“(“后跟anything后跟007,后跟anything”)”后跟anything,则解析为true 换句话说:*(*007*)* 我当前的模式如下所示: Pattern.compile("\\*\\s\\(\\s\\*\\s(0\\s0\\s7\\s)\\*\\s\\)\\*"); 然而,在我的229个junit测试用例中,大约有84个失败了 .*\(.*007.*\).* 转义为Java .compile(".*\\

我试图实现一个方法来接受一个字符串,如果它包含:anything后跟“(“后跟anything后跟007,后跟anything”)”后跟anything,则解析为true

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

我当前的模式如下所示:

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

然而,在我的229个junit测试用例中,大约有84个失败了

.*\(.*007.*\).*

转义为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));

这难道不是一件简单的事情吗

.*\(.*007.*\).*

转义为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));

我的op中的单\应该是双\,它一定在我发帖时转义为一为什么所有的
\s
都没有提到空格?测试用例是什么?注意,你可以用它来测试这些类型的表达式。我的op中的single\应该是double\,当我发帖时它一定转义为1。为什么所有的
\s
都没有提到空格?测试用例是什么?注意:您可以使用测试这些类型的表达式。