我使用正则表达式
r="[^A-Za-z0-9]+";
检测一个字符串是否有一个或多个除字母和数字以外的字符;
然后我尝试了以下方法:
Pattern.compile(r).matcher(p).find();
我测试了:
! @ # $ % ^ & * ( ) + =- [ ] \ ' ; , . / { } | " : < > ? ~ _ `
大多数情况下,除了后挡板\和插入符号 ^ 之外,它都能正常工作。
例如
String p = "abcAsd10^" (return false)
String p = "abcAsd10\\" (return false)
有什么我想念的吗?
最佳答案
以下代码在我编译并运行时打印“Found: true”:
class T
{
public static void main (String [] args)
{
String thePattern = "[^A-Za-z0-9]+";
String theInput = "abskKel35^";
boolean isFound = Pattern.compile(thePattern).matcher(theInput).find();
System.out.println("Found: " + isFound);
}
}
不确定为什么您会看到不同的结果...
关于Java检测字符串中的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10902911/