regular expression to convert state names to abbre

2019-08-24 04:07发布

问题:

I'm working on a project that requires only the use of regular expression to convert state names (must be case insensitive) to their two letter abbreviations.

I cannot use any sort of development environment or link to any databases or xml or ini files.

Please help!

回答1:

Since states don't have something regular in them regular expressions is the WRONG tool. I would suggest getting a new project.

However, the only solution (apart from stupid illogical hacks) is to hardcore every state:

s/Alabama/Al/
s/Alaska/Ak/
...
s/Wyoming/Wy/

A list of the states and their abbreviations can be found here.