Q:

Regex help - match specific string but not when wrapped in [!foobar!]

In PHP we want to replace strings with links but want to give content editors the ability to say don't automatically replace this string (i.e. give them a chance to wrap the string in some specific not characters so it doesn't fall into our logic pattern)
e.g. 
"I went for a walk in the park and came across a foobar"
will fall into our match and be replaced 
However
"I went for a walk in the park and came across a [!foobar!]"
will not.
Foobar - should match
[!Foobar!] - shouldn't match
Foobar!] - should match
[!Foobar - should match
Foobar, is cool - should match

Having trouble building this regex on http://regexr.com

A:

If I understood right, this should do the trick:
(?!\[!)(foobar)(?!!\])

See Live Demo

