-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
  • 149
  • 3
  • 15

2 Answers2

0

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

"[\\[\\]]"

DEMO

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
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