$re = '/^(?<an>[A-Z]{1,2}\d{10,11})\s+\b(?<pn>[\w\-\' ,\.]+?) (?<loc>\b[A-Z1-9][A-Z0-9\-–—]+) (?<rm>\b\w+ )?(?<bd>\b\w+ )?(?i:Primary Dx)/m';
$str = 'V12345678910 last, first MI D-321 D123 1 Primary Dx
V12345678910 last, first I D-321 D123 1 Primary Dx
V12345678910 last, first I Jr. D-321 D123 1 Primary Dx
V12345678910 last, first I III D-321 D123 1 Primary Dx
V12345678910 last, first I Jr. D-321 D123 1 Primary Dx
V12345678910 last, first-first2 D-321 D123 Primary Dx
V12345678910 l\'astlast2, first MI D-321 D123 Primary Dx
V12345678910 last, first II D-321 D123 1 Primary Dx
V12345678910 last-last2, first-first2 321 D123 1 Primary Dx
V12345678910 last-last2, first-first2 I D-321 D123 1 Primary Dx
V12345678910 last-last2, first-first2 I D-321 D123 Primary Dx
V12345678910 last-last2, first-first2 I ED Primary Dx
V12345678910 last-last2, first I ED Primary Dx
V12345678910 last-last2, first-first2 ED PRIMARY DX
V12345678910 LAST,FIRST FIRST ARU P102 1 Primary Dx
V12345678910 last, first D321 D123 1 Primary Dx
V12345678910 last, first-first2 first3 D321 Primary Dx
V12345678910 last, first-first2 D-321 123 Primary Dx
V12345678910 last, first-first2 D-321 123 Primary Dx
V12345678910 last, first-first2 D-321 123 1 Primary Dx
V12345678910 last, first-first2 D JIP LC A Primary Dx
VN2345678910 last,first first2 D JIP Primary Dx
These are a few assumptions made:
First match is the Account Number named <an>
2nd match is the Patient name named <pn>
There is 1 space between the end of the patient\'s name and the beginning of the Location. and between Location and Room, and between Room and Bed
There will always be a Location (Room and Bed may or may not exist).
3rd match is the Location named <loc>
Location may contain "-"
No spaces in Location
No spaces in Room
No spaces in Bed
4th match is the Room named <rm>
Room or Bed does not contain "-"
The Room may or may not exist
5th match is the Bed named <bd>
Bed can not exist without a Room
Room can not exist without a Location
Use Alt+0150 (en dash), Alt+0151 (em dash), or Alt+8722 (minus sign) using the numeric keypad.
Alt+0150 (en dash) –
Alt+0151 (em dash) —
(minus sign) -
';
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