验证除逗号之外的特殊字符的字符串

时间:2014-05-01 16:34:03

标签: java regex

我一直在尝试使用除逗号之外的特殊字符验证字符串。

这就是我所做的:

        if (str.matches("[A-Z0-9, ]*")) 
               System.out.println("Special char present");

但它无法正常工作。如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您似乎忘了否定自己的情况,因为它现在只检查字符串是否只包含A-Z 0-9 , 。如果你想要相反的条件尝试

if (!str.matches("[A-Z0-9, ]*")) 
//  ^- add this exclamation mark which represents negation
    System.out.println("Special char present");
相关问题