2013-03-05 49 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