Created
May 12, 2016 22:44
-
-
Save jrunning/44e1827b5ec56909518fa98fb98d759d to your computer and use it in GitHub Desktop.
A regular expression that matches every U.S. state or territory's name or postal abbreviation, more or less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
expr = /^( | |
a( | |
[klsz] | | |
( | |
l(abam|ask)? | | |
rizon | |
)a | | |
merican\ssamoa | | |
r(kansas)? | |
) | | |
c( t | | |
a(lifornia)? | | |
o(lorado|nnecticut)? | |
) | | |
d( c | | |
e(laware)? | | |
istrict\sof\scolumbia | |
) | | |
fl(orida)? | | |
gu(am)? | | |
h(awai)?i | | |
i( n | | |
d(aho)? | | |
l(linois)? | | |
(ndian|ow)?a | |
) | | |
k( | |
(ansa)?s | | |
(entuck)?y | | |
) | | |
m( | |
[edsntp] | | |
a(ine|ryland|ssachusetts)? | | |
i( | |
chigan | | |
ss(issipp|our)i | | |
nnesota | |
)? | | |
o(ntana)? | |
) | | |
n( | |
[vhjmycd] | | |
e( | |
(brask|vad)a | | |
w\s(hampshire|jersey|mexico|york) | |
)? | | |
orthern\smariana\sislands | |
) | | |
(nor|sou)th\s(carolin|dakot)a | | |
o( | |
h(io)? | | |
k(lahoma)? | | |
r(egon)? | |
) | | |
p( r | | |
(ennsylvani)?a | | |
uerto\srico | |
) | | |
r(i|hode\sisland) | | |
s[cd] | | |
t( | |
[nx] | | |
e(nnessee|xas) | |
) | | |
u( m | | |
nited\sstates\sminor\soutlying\sislands | | |
t(ah)? | |
) | | |
v( | |
(ermon)?t | | |
i(rgin\sislands)? | |
) | | |
w( | |
[avi] | | |
(ashingto|isconsi)n | | |
y(oming)? | |
) | | |
( v | | |
l(ouisian)? | | |
g(eorgi)? | | |
(west\s)?virgini | |
)a | |
)$/ix | |
input = STDIN.flat_map {|line| line.chomp.split(?|) } | |
input.each do |str| | |
if str =~ expr | |
puts "😃 #{str}" | |
else | |
puts "😡 #{str}" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
DC|District of Columbia | |
AS|American Samoa | |
GU|Guam | |
MP|Northern Mariana Islands | |
PR|Puerto Rico | |
UM|United States Minor Outlying Islands | |
VI|Virgin Islands |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment