A library for cleaning and simplifying file names?

I would like to convert file names that have spaces and characters containing only the characters AZ, az, period, hyphen and underscore. Something like this regular expression: ([az][AZ]-_\.)+

Of course, I could just do it with a regex. But since I already included many projects in my project (Spring, Hibernate, Apache Commons and much more), I was wondering if something like this is already available.

So the line is like this:

 This>is some(string,with $invalid*-chars).jpg 

Will be converted to this:

 This_is_some_string_with__invalid_-chars_.jpg 
+4
source share
1 answer

No library can offer you something simpler than String.replaceAll("[^a-zA-Z0-9-_\\.]", "_")

+9
source

Source: https://habr.com/ru/post/1347013/


All Articles