正则表达式选择不包含两个单词之间的单词的字符串[重复]

assume we have 4 strings:

1)
aa bb aa

2)
aa cc aa

3)
aa
bb
aa

4)
aa
cc
aa

I want to select only 1 and 3 to replace by php preg_replace.

After hours of googling I ended up with aa(?!.*cc).*aa that selects only 1 . is there any way to select 1 and 3 together?

</div>

"Not containing a word between two delimiters" can be done using a negative lookahead assertion:

(?s)aa(?:(?!cc).)*?aa

The (?s) modifier allows the . to also match newline characters.