Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Library to clean up and simplify filenames?

I'd like to convert filenames that have spaces and symbols to contain only the characters A-Z, a-z, a period, a hyphen, and an underscore. Something like this regex: ([a-z][A-Z]-_\.)+

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

So a string like this:

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

Would be converted to this:

This_is_some_string_with__invalid_-chars_.jpg
like image 270
Tauren Avatar asked Apr 07 '11 00:04

Tauren


1 Answers

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

like image 141
matt b Avatar answered Oct 21 '22 08:10

matt b