Reputation: 21
Hi I tried using this regular expression
^(?=[^,]+,[^,]+$)[a-zA-Z,]{1,20}$
But it didn't work.
Is there any way to achieve this?
Mean to say that,while saving the file from file writting, file name should be less than or equal to 20 characters long and characters after 20 should get trimmed.
Upvotes: 1
Views: 345
Reputation: 43043
You can perform the check using regular expression only with a small enhancement in the regex (see stribizhev's comment).
private static final Matcher FILENAME_CHECKER = Pattern.compile("^(?=[^,]+,[^,]+$)([a-zA-Z,]{1,20})[a-zA-Z,]*$").matcher("");
/**
*
* ...filename requirements go here...
*
* @param unsanitizedFilename the filename to sanitize
* @return a sanitized filename
*
* @throws IllegalArgumentException If the filename doesn't honor all requirements.
*/
public static String sanitizeFilename(String unsanitizedFilename) {
FILENAME_CHECKER.reset(unsanitizedFilename);
if (FILENAME_CHECKER.find() == false) {
throw new IllegalArgumentException("Invalid filename: " + unsanitizedFilename);
}
return FILENAME_CHECKER.group(1);
}
If you're using Java 7+, you can use the version below and take advantage of named groups.
private static final Matcher FILENAME_CHECKER = Pattern.compile("^(?=[^,]+,[^,]+$)(?<valid_portion>[a-zA-Z,]{1,20})[a-zA-Z,]*$").matcher("");
public static String sanitizeFilename(String unsanitizedFilename) {
FILENAME_CHECKER.reset(unsanitizedFilename);
if (FILENAME_CHECKER.find() == false) {
throw new IllegalArgumentException("Invalid filename: " + unsanitizedFilename);
}
return FILENAME_CHECKER.group("valid_portion");
}
The Matcher
calss is not thread safe. If thread safety is a concern in your case, here is the thread safe version relying on the Pattern
class.
private static final Pattern FILENAME_CHECKER = Pattern.compile("^(?=[^,]+,[^,]+$)([a-zA-Z,]{1,20})[a-zA-Z,]*$");
/**
*
* ...filename requirements go here...
*
* @param unsanitizedFilename the filename to sanitize
* @return a sanitized filename
*
* @throws IllegalArgumentException If the filename doesn't honor all requirements.
*/
public static String sanitizeFilename(String unsanitizedFilename) {
Matcher m = FILENAME_CHECKER.matcher(unsanitizedFilename);
if (m.find() == false) {
throw new IllegalArgumentException("Invalid filename: " + unsanitizedFilename);
}
return m.group(1);
}
Upvotes: 0
Reputation: 14015
You don't need regexp for trim string:
if(fileName.length()>20){
filename = filename.substring(0, 20);
}
Upvotes: 1