$re = '/#\d+\s[A-Z]\d+(?:[,-][A-Z]\d+)*(?:;#\d+\s[A-Z]\d+(?:[,-][A-Z]\d+)*)*/m';
$str = '#2 A4-B7;#4 A1,B9 or #66 A1 or #88 C7 or #9999 E78-F39 or #567 E74;#678 Q7
#6 A1,B9,C7;#77 A89-Z78;#77 A89-Z78';
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