Q:

.NET regex not matching with negative assertion

I have a file in which I need to match strings of the form DEFAULT_DATABASE = [db_name] only if the db_name is not master.
I'm using the following regex:
DEFAULT_DATABASE\s*=\s*\[(?!master)\]
but the negative assertion doesn't match. There are 0 matches no matter what db_name. What am I doing wrong? Here's my test code:
var inputs = new string [] {
    "some text DEFAULT_DATABASE = [master] more [more] text",
    "some text DEFAULT_DATABASE = [masters] more [more] text",
    "some text DEFAULT_DATABASE = [abc] more [more] text"
};

var re = new Regex(@"DEFAULT_DATABASE\s*=\s*\[(?!master)\]");

foreach (var input in inputs) {
    if (re.IsMatch(input)) {
        Console.WriteLine($"Match: {input}, value: {re.Match(input).Value}");
    }
}

A:

You'll want to include the closing bracket in the negative assertion and then allow for valid text before the closing bracket:
    var re = new Regex(@"DEFAULT_DATABASE\s*=\s*\[(?!master\]).*?\]");

Edit: The pattern must be non-greedy.

