Regex to match strings that have illegal file names

I tried to figure out how this exploded regular expression for two hours !!! At midnight I have to understand this and go to bed!

String str = new String("filename\\");
if(str.matches(".*[?/<>|*:\"{\\}].*")) {
    System.out.println("match");
}else {
    System.out.println("no match");
}

".*[?/<>|*:\"{\\}].*"- my expression in the expression. He catches everything correctly, except for the backslash !!! I need to know how to catch the backslash correctly, please help!

FYI, the illegal characters I'm trying to catch is this? \ / <> | *: "I have a working exception for the backslash

+3
source share
2 answers

, \\ Java, . :

if ("ab\\d".matches("[abd\\\\]*") {
  // match
}

Java String :

[abc\\]*

\\ , .

+8

, , , , :

, . , ., . :

^(\s+.*|.*[\\/:\"?*|<>].*|.*\s+||.*\.)$
+3

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


All Articles