Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我希望我的正则表达式匹配除.and之外的任何带有“特殊”字符的字符串/。其他特殊字符在某种黑名单上。但是,在运行时,我得到一个Illegal repetition错误。我该如何解决?
.
/
Illegal repetition
Pattern regex = Pattern.compile("!@#$%^&*()-_+=|\\}]{[\"':;?><,"); Matcher matcher = regex.matcher(key); if (matcher.find()) { return false; }
可能最好只指定允许的内容而不是拒绝的内容:
Pattern regex = Pattern.compile ("^[\\w\\s\\./]*$"); if (!regex.matcher(key).matches ()) return false;
这仅允许字母、数字、空格、点 ('.') 和斜杠 ('/')。