Regexp for a string containing only letters, numbers and space in Java

Requirement: A string must contain only letters, numbers and a space.
           I have to pass the clean name to another API.

Implementation: Java

I came up with this for my requirement

public static String getCleanFilename (String filename) {
    if (filename == null) {
        return null;
    }
    return filename.replaceAll ("[^ A-Za-z0-9]", "");
}

This works well for several of my test systems, but I want to know if there are not enough boundary conditions or some better way (in performance) to do this.

+3
source share
2 answers

, \t "". \s ([...\s] .

, , , . , , , .

EDIT:
, SHA-2 . .

+1

: , , ( ...).

: , + :

[^A-Za-z0-9 ]+

+2

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


All Articles