Skip to main content
2 of 2
Second stab at the RE
Chris Davies
  • 128k
  • 16
  • 178
  • 323

Let's decode the RE \[[^\]]*\]

  • \[ - Literal [ character
  • [^\] - Not \
  • ] - Literal ] character
  • * - Previous item repeated zero or more items, i.e. ] zero or more times
  • \] - Another literal ] character (the backslash is ignored here)

Applying this to [ A ] we can see it will not match. I suspect the question you're asking is why [^\]] does what it does. The ^ negation symbol has a special case that when the next symbol is ] it's treated literally, otherwise it's always the end of the [...] construct.

Instead you could use this RE, \[[^]*] or even anchor the front and back of the string, ^\[.*]$

Chris Davies
  • 128k
  • 16
  • 178
  • 323