用于查找*(*007*)*的Java正则表达式不起作用,希望得到一些建议
我试图实现一个方法来接受一个字符串,如果它包含:anything后跟“(“后跟anything后跟007,后跟anything”)”后跟anything,则解析为true 换句话说:用于查找*(*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(".*\\
*(*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
都没有提到空格?测试用例是什么?注意:您可以使用测试这些类型的表达式。