Regex for 50 US States

Need a regular expression to recognize the 50 US state names or USPS abbrevations? Here it is!

State Names and Abbreviations

This regex will match all 50 US state names (e.g., Texas, California) or USPS abbreviations (e.g., TX, CA) case insensitively.

(?i)\b(?:Alabama|AL|Alaska|AK|Arizona|AZ|Arkansas|AR|California|CA|Colorado|CO|Connecticut|CT|Delaware|DE|Florida|FL|Georgia|GA|Hawaii|HI|Idaho|ID|Illinois|IL|Indiana|IN|Iowa|IA|Kansas|KS|Kentucky|KY|Louisiana|LA|Maine|ME|Maryland|MD|Massachusetts|MA|Michigan|MI|Minnesota|MN|Mississippi|MS|Missouri|MO|Montana|MT|Nevada|NV|New\s+Hampshire|NH|New\s+Jersey|NJ|New\s+Mexico|NM|New\s+York|NY|North\s+Carolina|NC|North\s+Dakota|ND|Ohio|OH|Oklahoma|OK|Oregon|OR|Pennsylvania|PA|Rhode\s+Island|RI|South\s+Carolina|SC|South\s+Dakota|SD|Tennessee|TN|Texas|TX|Utah|UT|Vermont|VT|Virginia|VA|Washington|WA|West\s+Virginia|WV|Wisconsin|WI|Wyoming|WY|Nebraska|NE)\b

If you want the regex to match case sensitively, then simply remove the leading (?i) from the beginning of the above pattern.

State Abbreviations Only

This regex will match all 50 US state USPS abbreviations (e.g., TX, CA) case sensitively.

\b(?:AL|AK|AZ|AR|CA|CO|CT|DE|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY|NE)\b

If you want the regex to match case insensitively, then simply add a leading (?i) to the beginning of the above pattern.

State Names Only

This regex will match all 50 US state names (e.g., Texas, California) case insensitively.

(?i)\b(?: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|Nevada|New\s+Hampshire|New\s+Jersey|New\s+Mexico|New\s+York|North\s+Carolina|North\s+Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|Rhode\s+Island|South\s+Carolina|South\s+Dakota|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West\s+Virginia|Wisconsin|Wyoming|Nebraska)

If you want the regex to match case sensitively, then simply remove the leading (?i) from the beginning of the above pattern.

Data Source

The data was generated from this spreadsheet of the 50 state names and USPS abbreviations, which was copied from the USPS website.

License

Just in case anyone feels like they need a license, please consider this regex and file in the public domain under the Creative Commons CC0 license.