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EN|G[PS]|HN|J
M|L[BEV]|MR|OM|PM|RB|SD|TP|UR|V[KN]|W[EZ]|XM|Y[CR])|T(C[A
D]|GO|HA|JK|K[LM]|LS|ON|TO|U[NRV]|WN|ZA)|U(GA|KR|MI|RY|SA
|ZB)|V(AT|CT|EN|GB|IR|NM|UT)|W(LF|SM)|YEM|Z(AF|MB|WE)$/ix
def self.valid_iso_alpha2?(code)
!(code =~ /\A[A-Z]{2}\Z/i && code =~ ISO_3166_ALPHA_2).nil?
end
def self.valid_iso_alpha3?(code)
!(code =~ /\A[A-Z]{3}\Z/i && code =~ ISO_3166_ALPHA_3).nil?
end
def self.valid_iso?(code)
valid_iso_alpha2?(code) || valid_iso_alpha3?(code)
end
end
CountryCode.valid_iso?('Fra') #=> true
CountryCode.valid_iso?('USA') #=> true
CountryCode.valid_iso_alpha3?('US') #=> false
CountryCode.valid_iso?('USX') #=> false
为了好玩,我试图找到一个简短的正则表达式来验证Alpha-2 ISO代码:
# wget http://data.okfn.org/data/core/country-list/r/data.json
require 'json'
codes = JSON.parse(File.read('data.json')).map { |country| country['Code'] }.sort
regex = codes.group_by { |code| code[0] }.map do |first_letter, codes|
second_letters = codes.map { |code| code[1] }
regex_1 = codes.join('|')
regex_2 = first_letter + '[' + second_letters.join + ']'
regex_3 = first_letter + '[^' + (('A'..'Z').to_a - second_letters).join + ']'
[regex_1, regex_2, regex_3].min_by(&:size)
end.join('|')
regex = Regexp.new('^' + regex + '$')
p regex
# => /^A[^ABCHJKNPVY]|B[^CKPUX]|C[^BEJPQST]|D[EJKMOZ]|E[CEGHRST]|F[IJKMOR]|G[^CJKOVXZ]|H[KMNRTU]|I[DELMNOQRST]|J[EMOP]|K[EGHIMNPRWYZ]|L[ABCIKRSTUVY]|M[^BIJ]|N[ACEFGILOPRUZ]|OM|P[^BCDIJOPQUVXZ]|QA|R[EOSUW]|S[^FPQUW]|T[^ABEIPQSUXY]|U[AGMSYZ]|V[ACEGINU]|WF|WS|YE|YT|Z[AMW]$/
p ('AA'..'ZZ').select { |two_letters| two_letters =~ regex } == codes
#=> true
该脚本按首字母对所有代码进行分组,并查找列出第二个字母的最短方式。
因此,对于双字母国家/地区代码,您可以使用以下命令进行验证:
iso_3166_alpha_2 = /^A[^ABCHJKNPVY]|B[^CKPUX]|C
[^BEJPQST]|D[EJKMOZ]|E[CEGHRST]|F[IJKMOR]|G[^CJ
KOVXZ]|H[KMNRTU]|I[DEL-OQ-T]|J[EMOP]|K[EGHIMNPR
WYZ]|L[ABCIKR-VY]|M[^BIJ]|N[ACEFGILOPRUZ]|OM|P[
AE-HK-NRSTWY]|QA|R[EOSUW]|S[^FPQUW]|T[^ABEIPQSU
XY]|U[AGMSYZ]|V[ACEGINU]|WF|WS|YE|YT|Z[AMW]$/ix
注意:您应首先检查代码是否仅包含字母。否则“B%”会匹配。
3个字母代码的重复次数明显减少,因此正则表达式难以压缩。我使用Trie来获得此正则表达式:
iso_3166_alpha_3 = /^A(BW|FG|GO|IA|L[AB]|ND|R[EGM]|SM|T[A
FG]|U[ST]|ZE)|B(DI|E[LNS]|FA|G[DR]|H[RS]|IH|L[MRZ]|MU|OL|
R[ABN]|TN|VT|WA)|C(A[FN]|CK|H[ELN]|IV|MR|O[DGKLM]|PV|RI|U
[BW]|XR|Y[MP]|ZE)|D(EU|JI|MA|NK|OM|ZA)|E(CU|GY|RI|S[HPT]|
TH)|F(IN|JI|LK|R[AO]|SM)|G(AB|BR|EO|GY|HA|I[BN]|LP|MB|N[B
Q]|R[CDL]|TM|U[FMY])|H(KG|MD|ND|RV|TI|UN)|I(DN|MN|ND|OT|R
[LNQ]|S[LR]|TA)|J(AM|EY|OR|PN)|K(AZ|EN|GZ|HM|IR|NA|OR|WT)
|L(AO|B[NRY]|CA|IE|KA|SO|TU|UX|VA)|M(A[CFR]|CO|D[AGV]|EX|
HL|KD|L[IT]|MR|N[EGP]|OZ|RT|SR|TQ|US|WI|Y[ST])|N(AM|CL|ER
|FK|GA|I[CU]|LD|OR|PL|RU|ZL)|OMN|P(A[KN]|CN|ER|HL|LW|NG|O
L|R[IKTY]|SE|YF)|QAT|R(EU|OU|US|WA)|S(AU|DN|EN|G[PS]|HN|J
M|L[BEV]|MR|OM|PM|RB|SD|TP|UR|V[KN]|W[EZ]|XM|Y[CR])|T(C[A
D]|GO|HA|JK|K[LM]|LS|ON|TO|U[NRV]|WN|ZA)|U(GA|KR|MI|RY|SA
|ZB)|V(AT|CT|EN|GB|IR|NM|UT)|W(LF|SM)|YEM|Z(AF|MB|WE)$/ix
答案 1 :(得分:1)
您好,您始终可以使用此GEM Gem Github Link ...希望这可以解决您的问题。
答案 2 :(得分:1)
我为你做了一个宝石,https://github.com/jackbit/validiso,如果我是对的,请告诉我。感谢。
<强>用法强>
按代码搜索
Validiso::Countries.find_code('971')
按alpha2搜索
Validiso::Countries.find_alpha2('AE')
按alpha3搜索:
Validiso::Countries.find_alpha3('ARE')
如果不存在则返回nil,但如果成功则返回以下对象
{
"country_code": "971",
"alpha2": "AE",
"alpha3": "ARE",
"name": "United Arab Emirates",
"continent": "Asia"
}