String name= "12E463~1.jpg"; String newName = name.replaceAll("[a-zA-Z1-90_\\- \\.]*","_"); String name has some value which contains some special characters. I want all characters apart from A-Z a-z 0-9 . (dot) _ (underscore) - (hyphen) to be replaced with an _ (underscore) So I should get 12E463_1.jpg in newName But using the above regex the opposite happens. All characters apart from the special ...