In Java, I\'ve a File-Name-String. There I want to replace all illegal Characters with \'_\', but not a-z, 0-9, -,. and <
a-z
0-9
-
.
You need to replace everything but [a-zA-Z0-9.-]. The ^ within the brackets stands for "NOT".
[a-zA-Z0-9.-]
^
myString = myString.replaceAll("[^a-zA-Z0-9\\.\\-]", "_");