2

Missing closing bracket in character class near index 13 |\?*<":>+[]/' My code:

Pattern.compile("|\\?*<\":>+[]/'").matcher(name).matches()
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

2

You may use

Pattern.compile("[|\\\\?*<\":>+\\[\\]/']+").matcher(name).matches()

The regex means:

  • [ - start of a positive character class:
    • | - a pipe
    • \\ - a backslash (requires additional backslashes in the string literal, "\\\\")
    • ? - a question mark
    • * - an asterisk
    • < - an open angle bracket
    • " - a double quotationmark
    • : - a colon
    • > - a close angle bracket
    • + - a plus
    • \[ - a [ char (must be escaped when [ is inside a character class)
    • \] - a ] char (must be escaped when ] is inside a character class)
    • / - a forward slash
    • ' - a single quotation mark
  • ]+ - end of character class, 1 or more occurrences.

So, this will validate a string that only consists of 1 or more occurrences of these chars. If you need the opposite, add ^ after the first [:

Pattern.compile("[^|\\\\?*<\":>+\\[\\]/']+").matcher(name).matches()
//                ^ 

Java demo:

String name = "Wiktor Stribiżew";
System.out.println(Pattern.compile("[^|\\\\?*<\":>+\\[\\]/']+").matcher(name).matches());
// => true
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563