use warnings; use strict; use Test::More; my $regex = qr{ \b INSERT \b (?: # the next thing may not be one of these: (?! \b SELECT \b | \( | \; ) . )+ # match one or more times \b doc \b # target string }imsx; like "INSERT doc (", $regex; like "INSERT INTO doc bar (", $regex; unlike "INSERT foo ( doc", $regex; unlike "INSERT foo SELECT doc", $regex; unlike "INSERT foo ; doc", $regex; done_testing;