Java 未检测到空白的数量

Java 未检测到空白的数量,java,regex,Java,Regex,我一直在尝试使输入能够接受“)”括号后的任意数量的白线。我已经查找了regex网站,“\s+”应该可以完成这项工作,但在我的示例中,它不起作用 以下是我得到的: String data5 = "Relation(Attribute) "; String regexAlt = "([a-zA-Z0-9-_]+\\([a-zA-Z0-9,-_ ]+\\)+[\n|\\s\n|\\s+])+"; if(data5.matches(regexAlt)){ System.out

我一直在尝试使输入能够接受“)”括号后的任意数量的白线。我已经查找了regex网站,“\s+”应该可以完成这项工作,但在我的示例中,它不起作用

以下是我得到的:

String data5 = "Relation(Attribute)           ";
String regexAlt = "([a-zA-Z0-9-_]+\\([a-zA-Z0-9,-_ ]+\\)+[\n|\\s\n|\\s+])+";
if(data5.matches(regexAlt)){
    System.out.println("yes");
}
    else{
        System.out.println("No");
}
它不断输出“否”。 有什么建议吗

应该是:

String regexAlt = "([a-zA-Z0-9-_]+\\([a-zA-Z0-9,-_ ]+\\)+[\\n\\r\\s]+)";
                                                            ^  ^  ^ ^

这个很好用

System.out.println(Pattern.compile("(\\d+|\\w+)\\W?\\w+\\W?\\s+").matcher(data5).matches());

尝试用\s+替换[\n|\\s\n|\\s+])+并首先删除(非常感谢。我想问,为什么它是[\\n\\r\\s]+?它的工作原理是否与使用“|”相同?如果您使用characterclass
[]
则不需要在其中使用管道(
)。另外,您没有在
[]之后立即使用
+
。]
这就是您的正则表达式不匹配的原因。啊,好吧,现在有意义了。再次感谢您打印出您想要的
True