如果允許使用者隨意輸入, 容易有風險, 如果只允許大小寫英文與減號, 寫法如下:
String filename = "abc.xyz";
String pattern = "^[-a-zA-Z0-9]+$";
boolean is_match_pattern = filename.matches(pattern);
不知道為什麼, pattern = “^[-a-zA-Z0-9]+” 的效果在 java 裡與 pattern = “^[-a-zA-Z0-9]+$” 相同, 在 javascript 裡, 可以測出不同的結果.
相關文章
How can I allow one space, A-Z, a-z, 0-9 in regex?
https://stackoverflow.com/questions/9609356/how-can-i-allow-one-space-a-z-a-z-0-9-in-regex
java string match pattern with a-z , A-Z, 0-9 and some special char
https://stackoverflow.com/questions/35265988/java-string-match-pattern-with-a-z-a-z-0-9-and-some-special-char
Javascript 字串, 只允許特定白名單
https://stackoverflow.max-everyday.com/2023/09/javascript-string-match-regex/