9

I want to make a negated character class to match a square bracket tag like this [square bracket tag]. The problem is, the ] character ends the character class!

I tried

\[[^\]]+]

but I get a syntax error when I run it. (This is in the find and replace regex engine which is slightly different than the standard .NET engine fyi).

David Moles
  • 43,504
  • 25
  • 125
  • 223
just.another.programmer
  • 8,189
  • 8
  • 46
  • 86

3 Answers3

3

You forgot to escape the final end bracket:

\[[^\]]+\]
Damien_The_Unbeliever
  • 227,877
  • 22
  • 326
  • 423
0

The first example in msdn uses \\ for escaping the \ which then escapes the .. So you should do something like \\[[^\\]]+\\] and also as Damien_The_Unbeliever said you haven't closed the final bracket.

bliof
  • 2,892
  • 2
  • 22
  • 38
-1

I definitely expected escaping with "\" but it didn't work for me (grep@MacOS) but this: [^]] did the job. Just place ] as the first character in class. I actually used something like: [^]?[]

krzank
  • 1