(?<!\w) # Make sure state not preceeded by text
(?:,\s+)? # Optionally match if followed by a , and several whitespace
(?:A[LKZR] # Start matching every combination of US state. Use -i flag to ignore case.
|
C[AOT] # E.g. this will match CA/CO/CT
| # or
DE # DE
| # or
FL # etc...
|
GA
|
HI
|
I[ADLN]
|
K[SY]
|
LA
|
M[EDAINSOT]
|
N[EVHJMYCD]
|
O[HKR]
|
PA
|
RI
|
S[CD]
|
T[NX]
|
UT
|
V[AT]
|
W[AIVY])
,? # Match if , at the end
\s? # Match if whitespace at the end (was in your examples)
(?!\w) # Make sure state is not succeeded by letters
检查此表达式:
Regex Demo
这个表达式很长,所以让我分几个部分来解释:
别忘了设置-i标志,它将忽略字母大小写。你知道吗
/,(PA | FL |…)/i
我将剩下的48个州作为练习留给读者;)
相关问题 更多 >
编程相关推荐