2012-11-15 42 views

回答

3

这是某事像那:

String regexp = "--+"; 

这是如何使用它:

 Pattern p = Pattern.compile(regexpr); 

    System.out.println(p.matcher("fsdfsa").matches()); //false 
    System.out.println(p.matcher("-").matches()); //false 
    System.out.println(p.matcher("--").matches()); //true 
    System.out.println(p.matcher("-----").matches()); //true 
+0

如果您只使用'matches'方法,则不需要手动编译模式,您可以使用[String]中定义的'matches'(http://docs.oracle.com/javase/ 7/docs/api/java/lang/String.html#matches(java.lang.String)) – jlordo

+0

在示例的上下文中,您是对的。但是,如果你打算多一次使用正则表达式,并且这不是微不足道的,就像在这种情况下一样预编译它并使用Pattern – bmichalik

0

正则表达式\--+将完成这项工作。

1

这是明确的连字符,而不是一个破折号,而不是一个减号。请参阅Unicode Hyphen characters

String pattern = "[\u2010]{2,}"; 

您可以将所有不同的连字符添加到方括号中。下面的量词说2次或更多次。

相关问题