You can use negative lookaheads:
/^(?!.*(bolt|nut)s?).*(black|union)/
/ : Starting delimiter
^ : Matches the start of the string
(?! : Start of negative lookahead
.* : Matches any character 0 or more times
: Matches a word boundary before the target word
(bolt|nut) : Literal match "bolt" OR "nut"
s? : Matches an optional "s"
: Matches a word boundary after the target word
) : End of negative lookahead
.* : Match any charachter 0 or more times
(black|union) : Literal match "black" OR "union"
/ : Ending delimiter
Using the
either side of the word means that you don't accidentally filter out words which contain the word bolt|nut
for example: bolted flange
.
$stringList = [
'0 - 2"x6" black nipple',
'0 - 1/2x4 black nipple',
'20 - 3/4" x 3/8" black bushing.',
'10 - 3/4" black plugs thread',
'0 - 7/8 x 3 3/4 black bolts',
'0 -7/8 black nuts'
];
foreach($stringList as $string){
var_dump(
preg_match('/^(?!.*(bolt|nut)s?).*(black|union)/', $string)
);
}
/* Output...
int(1)
int(1)
int(1)
int(1)
int(0)
int(0)
i.e. matches for all but the last 2!
*/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…