我正在尝试实现一种方法来接受一串字符,如果它包含以下内容,则解析为 true:任何内容,后跟“(”,后跟任何内容,后跟 007,后跟任何内容,后跟“)”,后跟任何内容。
换句话说:*(*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 正则表达式不起作用,希望得到一些建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49949096/