我只想将字符串中的一些特殊字符(<、>、%、$、^)列入黑名单,并允许其他所有字符。 我一直在尝试一些正则表达式,但它们不起作用。例如[^%<>] 但它不起作用。
String s = "script";
if(s.matches("[^%<>\\$]")){
System.out.println("valid");
}else{
System.out.println("invalid");
}
请提出一些解决方案
最佳答案
i want to blacklist only some special characters(<,>,%,$,^) from a string and allow everything else.
您可以使用此正则表达式:
^[^%<>^$]+$
PS:用于 String#matches
你甚至没有排列开始/结束 anchor ^ and $
并且可以使用这样的代码:
String s = "script";
if( s.matches("[^%<>^$]+") ) {
System.out.println("valid");
} else {
System.out.println("invalid");
}
关于java - 将一些特殊字符列入黑名单并允许其他所有字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20444484/