Skip to main content
1 of 2
Chris Davies
  • 128.1k
  • 16
  • 178
  • 323

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

  • \[ - Literal [ character
  • [^\]] - Neither a \ nor ] character
  • * - Previous item repeated zero or more items
  • \] - Literal ] character

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 to be treated as a literal. Unless you prefix it with \ in which case literal-vs-RE is reversed. This is what you've hit.

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

Chris Davies
  • 128.1k
  • 16
  • 178
  • 323