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
  • 48,006
  • 27
  • 136
  • 235
just.another.programmer
  • 8,579
  • 8
  • 51
  • 90

3 Answers3

3

You forgot to escape the final end bracket:

\[[^\]]+\]
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
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,957
  • 2
  • 23
  • 39
-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