-5

How do I write a Java regular expression that will find the character '[' or ']' in the expression? Since these are special characters in regular expressions, I'm stumped.

KSK
  • 139
  • 3
  • 15

2 Answers2

0

Try the below regex to match the literal [,] symbols,

"[\\[\\]]"

DEMO

Avinash Raj
  • 166,785
  • 24
  • 204
  • 249
0

Other than escaping, as that can get a little confusing I suppose, you can use the hex values of the characters:

[ = 5B
] = 5D

\x5B // For use in your expression