$re = '/^((?<atu>ATU)|(?<be>BE)|(?<bg>BG)|(?<cy>CY)|(?<cz>CZ)|(?<dk>DK)|(?<de>DE)|(?<ee>EE)|(?<el>EL)|(?<es>ES)|(?<fi>FI)|(?<fr>FR)|(?<hr>HR)|(?<hu>HU)|(?<ie>IE)|(?<lt>LT)|(?<lu>LU)|(?<lv>LV)|(?<mt>MT)|(?<nl>NL)|(?<pl>PL)|(?<pt>PT)|(?<ro>RO)|(?<se>SE)|(?<si>SI)|(?<sk>SK)|(?<xi>XI))((?(atu)[0-9]{8})|(?(be)[01][0-9]{9})|(?(bg)[0-9]{9,10})|(?(cy)[A-Z0-9]{9})|(?(cz)[0-9]{8,10})|(?(dk)[0-9]{8})|(?(de)[0-9]{9})|(?(ee)[0-9]{9})|(?(el)[0-9]{9})|(?(es)[A-Z]{1}[0-9]{8})|(?(fi)[0-9]{8})(?(fr)[0-9A-Z]{2}[0-9]{9})|(?(hr)[0-9]{11})|(?(hu)[0-9]{8})|(?(ie)(?:[0-9]{7}[A-Z]{1,2}|[0-9][A-Z][0-9]{5}[A-Z]))|(?(lt)(?:[0-9]{9}|[0-9]{12}))|(?(lu)[0-9]{8})|(?(lv)[0-9]{11})(?(mt)[0-9]{8})(?(nl)[0-9]{9}B[0-9]{2})|(?(pl)[0-9]{10})|(?(pt)[0-9]{9})|(?(ro)[0-9]{2,10})|(?(se)[0-9]{12})|(?(si)[0-9]{8})|(?(sk)[0-9]{10})|(?(xi)(?:[0-9]{3}[0-9]{4}[0-9]{2}(?:[0-9]{3})?|[A-Z0-9]{5})))$/m';
$str = 'ATU99999999
BE0999999999
BE1999999999
BG999999999
BG9999999999
CY99999999L
CZ99999999
CZ999999999
CZ9999999999
DE999999999
DK99999999
EE999999999
EL999999999
ESX12345678
FI99999999
FRXX999999999
HR99999999999
HU99999999
IE9S99999L
IE9999999WI
LT999999999
LT999999999999
LU99999999
LV99999999999
MT99999999
NL000099998B57
PL9999999999
PT999999999
RO999999999
SE999999999999
SI99999999
SK9999999999
XI999999999
XI999999999999
XIGD999
XIHA999';
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