.NET Regex Negative Lookahead - what am I doing wrong?
Assuming I have: StartTest NoInclude EndTest StartTest Include EndTest and am using: /StartTest(?!NoInclude)[\s\S]*?EndTest/g Why am I matching both groups? Regexr example: http://regexr.com/3db8m You fail the match with the lookahead if NoInclude appears straight after StartTest . You need a tempered greedy token : (?s)StartTest(?:(?!(?:Start|End)Test|NoInclude).)*EndTest ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ See the regex demo The regex is matching StartTest , then matches any text that is not StartTest , EndTest or NoInclude , up to the EndTest . Since the * is greedy, it will make the .