I need to validate a in a method like this.
validateFileName(Editable s) {
String filtered_str = s.toString();
if (filtered_str.matches(".*[regexp].*")) {
filtered_str = filtered_str.replaceAll("[regxp]", "");
s.clear();
s.append(filtered_str);}
Which regexps should i use to exclude all illegal characters and white-spaces? I'm using linux
If you're using a POSIX-compliant operating system, the legal characters in a file name are a-z, A-Z, 0-9, period, underscore, and hyphen. The regex to match 'illegal' characters would therefore be
[^-_.A-Za-z0-9]
Addendum: This is if you want a fully-portable file name. As I was corrected in Josip's comment below, POSIX itself actually allows more characters.
If your idea is only to exclude ilegal and space char you can use something like:
'^[^*&%\s]+$'
where you can add any "ilegal" char into the list of chars (in this case it ignores *, &, % and space) \s
is the space! The ^
inside the []
is part of the regex syntax it means: do not match any chars inside []
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With