$re = '/^z[0-3|6-8]{1}$|^z5(ab|c)$|^z12c$|^z1[5-7]$|^z20$/m';
$str = 'z0
z1
z2
z3
z4
z5
z5a
z5b
z5ab
z5c
z6
z7
z8
z9
z10
z11
z12
z12c
z13
z14
z15
z16
z17
z18
z19
z20';
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