I'm trying to build a regular expression that will detect any character that Windows does not accept as part of a file name (are these the same for other OS? I don't know, to be honest).
These symbols are:
\ / : * ? " |
Anyway, this is what I have: [\\/:*?\"<>|]
The tester over at http://gskinner.com/RegExr/ shows this to be working. For the string Allo*ha
, the *
symbol lights up, signalling it's been found. Should I enter Allo**ha
however, only the first *
will light up. So I think I need to modify this regex to find all appearances of the mentioned characters, but I'm not sure.
You see, in Java, I'm lucky enough to have the function String.replaceAll(String regex, String replacement). The description says:
Replaces each substring of this string that matches the given regular expression with the given replacement.
So in other words, even if the regex only finds the first and then stops searching, this function will still find them all.
For instance: String.replaceAll("[\\/:*?\"<>|]","")
However, I don't feel like I can take that risk. So does anybody know how I can extend this?
If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")
In both NTFS and FAT file systems, the special file name characters are: '\', '/', '. ', '?' , and '*'. On OEM code pages, these special characters are in the ASCII range of characters (0x00 through 0x7F).
Do not use the following reserved names for the name of a file: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed immediately by an extension; for example, NUL. txt is not recommended.
since no answer was good enough i did it myself. hope this helps ;)
public static boolean validateFileName(String fileName) { return fileName.matches("^[^.\\\\/:*?\"<>|]?[^\\\\/:*?\"<>|]*") && getValidFileName(fileName).length()>0; } public static String getValidFileName(String fileName) { String newFileName = fileName.replace("^\\.+", "").replaceAll("[\\\\/:*?\"<>|]", ""); if(newFileName.length()==0) throw new IllegalStateException( "File Name " + fileName + " results in a empty fileName!"); return newFileName; }
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