$re = '/LIST\d+ LIST\d+\R((?!$)\S*(?: +\S+)?(?:\R|\Z))*?(?:(?:(\S+) +(?:\S*\R(?1)*\S* +)?\2\b\R?)|(?:\S+ (\S+)\R(?1)*\3\b))(?1)*/m';
$str = 'LIST1 LIST2
item1 item2
item3 item4
item5 item6
LIST3 LIST4
item1 item2
item4
item2
LIST5 LIST6
item1 item3
item2 item4
item5 item2
LIST7 LIST8
item1 itemA
item2 itemB
item3 itemC
itemD
item3
itemE';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php