A State codes in the US are made up of two alphabets. The digits are used to identify a particular state. In this article let’s understand how we can create a regex for US State Code and how regex can be matched for US State Code.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Structure of US State Code
- State codes in the US are made up of two alphabets.
- The digits are used to identify a particular state.
- Both letters are capitalized.
Regex for checking if US State Code is valid or not
Regular Expression-
/^(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
ABC | does not match |
XYZ | does not match |
DE | matches |
FL | matches |
Here is the list of US state codes-
AL Alabama
AK Alaska
AZ Arizona
AR Arkansas
CA California
CO Colorado
CT Connecticut
DE Delaware
DC District of Columbia
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
Here is a detailed explanation of the above regex-
/^(A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])$/gm
1st Capturing Group (A[KLRZ]|C[AOT]|D[CE]|FL|GA|HI|I[ADLN]|K[SY]|LA|M[ADEINOST]|N[CDEHJMVY]|O[HKR]|PA|RI|S[CD]|T[NX]|UT|V[AT]|W[AIVY])
1st Alternative A[KLRZ]
A matches the character A with index 6510 (4116 or 1018) literally (case sensitive)
Match a single character present in the list below [KLRZ]
KLRZ matches a single character in the list KLRZ (case sensitive)
2nd Alternative C[AOT]
C matches the character C with index 6710 (4316 or 1038) literally (case sensitive)
Match a single character present in the list below [AOT]
AOT matches a single character in the list AOT (case sensitive)
3rd Alternative D[CE]
D matches the character D with index 6810 (4416 or 1048) literally (case sensitive)
Match a single character present in the list below [CE]
CE matches a single character in the list CE (case sensitive)
4th Alternative FL
FL matches the characters FL literally (case sensitive)
5th Alternative GA
GA matches the characters GA literally (case sensitive)
6th Alternative HI
HI matches the characters HI literally (case sensitive)
7th Alternative I[ADLN]
I matches the character I with index 7310 (4916 or 1118) literally (case sensitive)
Match a single character present in the list below [ADLN]
ADLN matches a single character in the list ADLN (case sensitive)
8th Alternative K[SY]
K matches the character K with index 7510 (4B16 or 1138) literally (case sensitive)
Match a single character present in the list below [SY]
SY matches a single character in the list SY (case sensitive)
9th Alternative LA
LA matches the characters LA literally (case sensitive)
10th Alternative M[ADEINOST]
M matches the character M with index 7710 (4D16 or 1158) literally (case sensitive)
Match a single character present in the list below [ADEINOST]
11th Alternative N[CDEHJMVY]
N matches the character N with index 7810 (4E16 or 1168) literally (case sensitive)
Match a single character present in the list below [CDEHJMVY]
12th Alternative O[HKR]
O matches the character O with index 7910 (4F16 or 1178) literally (case sensitive)
Match a single character present in the list below [HKR]
13th Alternative PA
PA matches the characters PA literally (case sensitive)
14th Alternative RI
RI matches the characters RI literally (case sensitive)
15th Alternative S[CD]
S matches the character S with index 8310 (5316 or 1238) literally (case sensitive)
Match a single character present in the list below [CD]
16th Alternative T[NX]
T matches the character T with index 8410 (5416 or 1248) literally (case sensitive)
Match a single character present in the list below [NX]
17th Alternative UT
UT matches the characters UT literally (case sensitive)
18th Alternative V[AT]
V matches the character V with index 8610 (5616 or 1268) literally (case sensitive)
Match a single character present in the list below [AT]
19th Alternative W[AIVY]
W matches the character W with index 8710 (5716 or 1278) literally (case sensitive)
Match a single character present in the list below [AIVY]
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Hope this article was useful to match US State Code number regex pattern. In conclusion, understanding regular expressions (regex) and their application in validating and matching US State Codes is essential. Regex provides a powerful means to search, validate, and manipulate text patterns. By delving into the structure of US State Codes and crafting an effective regex pattern, this article has demonstrated how regex can be employed to ensure accurate identification of valid US State Codes. By following the insights provided here, readers can effectively harness regex to handle similar text pattern recognition tasks.