PHP regex code for country and post code

Good post code for US; entered code was 11111.

There is a live online php regex tester here: https://www.phpliveregex.com

Regular expressions to match parts of an address

We always separate parts of an address by a comma; although usually the zip or postcode is only separated by a space. Search and extract in reverse order works best (zip, state, city, street)

US Zip Code

\b\d{5}(?:-\d{4})?\b

City

(?:[A-Z][a-z.-]+[ ]?)+

State

Alabama|Alaska|Arizona|Arkansas|California|Colorado|Connecticut|Delaware|Florida|Georgia|Hawaii|Idaho|Illinois|Indiana|Iowa|Kansas|Kentucky|Louisiana|Maine|Maryland|Massachusetts|Michigan| Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New[ ]Hampshire|New[ ]Jersey|New[ ]Mexico|New[ ]York|North[ ]Carolina|North[ ]Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|Rhode[ ]Island |South[ ]Carolina|South[ ]Dakota|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West[ ]Virginia |Wisconsin|Wyoming

US State abbreviations

AL|AK|AS|AZ|AR|CA|CO|CT|DC|DE|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY

Complete address:

{city pattern},[ ](?:{state pattern}|{state abbreviation pattern})[ ]{zip pattern}

Street address:

\d+[ ](?:[A-Za-z0-9.-]+[ ]?)+(?:Avenue|Lane|Road|Boulevard|Drive|Street|Ave|Dr|Rd|Blvd|Ln|St)\.?

Separate by \s to test for either a space or a line break and you're done!

Canada

Canadian Postal Codes

//Canadian Postal Codes
'/^([ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9])*$/'

Canadian Provinces

//Canadian Province Abbreviations
'/^(?:AB|BC|MB|N[BLTSU]|ON|PE|QC|SK|YT)*$/'

Australia

Australian Postal Codes

//Australian Postal Codes
'/^((0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2}))*$/'