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:
\ / : * ? " |
Replaces each substring of this string that matches the given regular expression with the given replacement.
Windows filename rules are tricky. You're only scratching the surface.
For example here are some things that are not valid filenames, in addition to the chracters you listed:
(yes, that's an empty string) . .a a. a (that's a leading space) a (or a trailing space) com prn.txt [anything over 240 characters] [any control characters] [any non-ASCII chracters that don't fit in the system codepage, if the filesystem is FAT32]
Removing special characters in a single regex sub like String.replaceAll() isn't enough; you can easily end up with something invalid like an empty string or trailing ‘.’ or ‘ ’. Replacing something like “[^A-Za-z0-9_.]*” with ‘_’ would be a better first step. But you will still need higher-level processing on whatever platform you're using.