2013-03-05 112 views
0

我希望我的正则表达式匹配除“.”和“/”以外的“特殊”字符的任何字符串。其他特殊字符在黑名单上。但是,在运行时,我得到一个Illegal repetition错误。我该如何解决这个问题?使用正则表达式限制特殊字符

Pattern regex = Pattern.compile("[email protected]#$%^&*()-_+=|\\}]{[\"':;?><,"); 
Matcher matcher = regex.matcher(key); 
if (matcher.find()) { 
    return false; 
} 
+0

请还包括一个标签,指定您正在使用的编程语言或工具。 – Johnsyweb 2013-03-05 05:59:05

回答

1

也许它会更好,只是规定什么是允许的,而不是什么被拒绝:

Pattern regex = Pattern.compile ("^[\\w\\s\\./]*$"); 
if (!regex.matcher(key).matches()) return false; 

这样只允许字母,数字,空格,点和斜线(‘(”。’) /')。

+0

谢谢。你救了我的一天。 – kitokid 2013-03-05 06:05:11