Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to come up with regex for US states (abbreviation and full name mixed all together)

Tags:

c#

regex

I guys. I'm trying to come up with a regex (C#) for eg: FL or Florida. I have [A-Z]{2} but some results are coming back empty because the patterns are mixed, full names are being used also. Thanks.

like image 261
Labanino Avatar asked Oct 13 '25 06:10

Labanino


1 Answers

The following regex will match what you are after:

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

I would not recommend it (per the comments above), but it will do the trick. It contains the names of all 50 states as well as their abbreviations, separated by or. Thus it will match any of these. You might consider adding word boundaries etc...

It is starting to look a lot like a lookup, isn't it?

like image 83
Floris Avatar answered Oct 14 '25 21:10

Floris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!