Solution 1:
preg_match_all('/(?P<state>[A-Z]{2,3})\s\d{4}$/m', $str, $matches);
var_dump($matches['state']);
Output:
array(3) {
[0] => string(3) "VIC"
[1] => string(3) "VIC"
[2] => string(3) "VIC"
}
This regex runs in multiline mode (/m) and captures:
(?P[A-Z]{2,3}) → a named group state containing 2–3 uppercase letters
\s → a whitespace character
\d{4}$ → exactly 4 digits at the end of a line
I used the group name state because in my case, these codes represent Australian states.