Skip to main content
Post Undeleted by Chris Davies
Second stab at the RE
Source Link
Chris Davies
  • 128k
  • 16
  • 178
  • 323

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

  • \[ - Literal [ character
  • [^\]][^\] - Neither aNot \ nor
  • ] - Literal ] character
  • * - Previous item repeated zero or more items, i.e. ] zero or more times
  • \] - LiteralAnother 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 to be treated as a literal. Unless you prefix it withliterally, otherwise it's always the end of the \[...] in which case literal-vs-RE is reversed. This is what you've hitconstruct.

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

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, ^\[.*]$

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, ^\[.*]$

Post Deleted by Chris Davies
Source Link
Chris Davies
  • 128k
  • 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, ^\[.*]$