$re = '~^(?<NAME>[\w ]+)\v
(?:^(?<ATTN>[\w ]+)\v)?
(?:^(?<UNIT>[\w ]+)\v)?
^(?<ADDRESS>[\w ]+)\v
^(?<CITY>.*)\h(?<STATE>[A-Z]{2})\h(?<ZIP>\d{5}(?:-\d{4})?)$~mx';
$str = 'TROOP LEADER
PSC BOX 12345
CAMP MATTERHORN NC 21212-1023
EXECUTIVE OFFICER
OFFICE OF COMMUNICATION
PSC BOX 67890
CAMP MATTERHORN NC 21212-1023
DIRECTOR
ENGINEERING DIV
UNIT 123456
FPO AP 96999-1234
COMMANDING OFFICER
BILLBOARD PAINTING STATION
3870 PLURBLE CT SUITE 110
JOSEPHINE MT 64513-9999
CORPUS CHRISTINA
ATTN HOOPLA CADRE
SECURITY DEPT 520
225 E 25TH AVE STE 500
SAN FRANCISCO FL 12345-8888';
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