爱尔兰 Eircode 验证
Validation for Irish Eircode
我想知道是否有验证爱尔兰 Eircode 格式的最佳实践。到目前为止,我在 JavaScript 中使用 REGEX 的最佳尝试是基于第 11 页上的官方规范 here.
(第 11 页基于文档中的页码,如果包含封面则为第 12 页)
/^[A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y]{1}[0-9]{1}[0-9,W]{1}[\ \-]?[0-9,A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y]{4}$/
我在这里没有找到任何与 Eircode 相关的问题,所以我想我会打开这个问题,看看其他人的想法,看看任何人都能想出什么 better/shorter/more 有效的模式。
编辑:根据@Asunez 的回答删除了逗号。
/^[ACDEFHKNPRTVWXY]{1}[0-9]{1}[0-9W]{1}[\ \-]?[0-9ACDEFHKNPRTVWXY]{4}$/
更新了这个答案,避免了 char B
。你可以试试这个:
/^[AC-Y]{1}[0-9]{1}[0-9W]{1}[ \-]?[0-9AC-Y]{4}$/
描述:
^ assert position at start of the string
[AC-Y]{1} match a single character present in the list below
Quantifier: {1} Exactly 1 time (meaningless quantifier)
A the literal character A (case sensitive)
C-Y a single character in the range between C and Y (case sensitive)
[0-9]{1} match a single character present in the list below
Quantifier: {1} Exactly 1 time (meaningless quantifier)
0-9 a single character in the range between 0 and 9
[0-9W]{1} match a single character present in the list below
Quantifier: {1} Exactly 1 time (meaningless quantifier)
0-9 a single character in the range between 0 and 9
W the literal character W (case sensitive)
[ \-]? match a single character present in the list below
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
the literal character
\- matches the character - literally
[0-9AC-Y]{4} match a single character present in the list below
Quantifier: {4} Exactly 4 times
0-9 a single character in the range between 0 and 9
A the literal character A (case sensitive)
C-Y a single character in the range between C and Y (case sensitive)
$ assert position at end of the string
由于@Manwal 的回答并没有完全做到它应该做的,这里是我尝试缩短 OP 的正则表达式:
(?:^[AC-FHKNPRTV-Y][0-9]{2}|D6W)[ -]?[0-9AC-FHKNPRTV-Y]{4}$
支持 A65 B2CD 邮政编码的更新版本 - (?:^[AC-FHKNPRTV-Y][0-9]{2}|D6W)[ -]?[0-9AC-FHKNPRTV-Y]{4}$
这就是您的正则表达式的基本内容,并进行了一些更改:
- 删除了逗号。您不需要逗号来列出
[]
括号内的项目。
- 在可能的情况下添加范围,并在其中节省一些 space(
C-F
、V-Y
)。在其他地方添加范围没有好处,因为它不会使正则表达式更短。
- 您不需要转义 space。正则表达式中的“”是文字。
- 如果破折号是字符 class 中的最后一个字符(方括号)
,则您也不需要转义破折号
- 正则表达式的第一部分现在位于非捕获组中,以允许或使用第三个位置唯一可能的字母“D6W”大小写。
也可以仅使用后视来处理 D6W
,但这比正则表达式更像是一门艺术。
参见正则表达式演示:here
您还可以将字符 class 反转为 不 包括给定字符,虽然它不会使正则表达式更短,但也值得注意。但是,您需要确保不包含其他字符(如点、逗号)。我通过添加 \W
标记来做到这一点。
你可以试试here
根据产品指南第 1.5.4 章,允许的标志是:
-----------------------------------------------------------------------
| Component | Position | Allowed characters |
-----------------------------------------------------------------------
| Routing Keys | 1 | A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
| Routing Keys | 2 | 0-9 |
-----------------------------------------------------------------------
| Routing Keys | 3 | 0-9 with the exception of W for D6W |
-----------------------------------------------------------------------
| Unique Identifier | 4 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
| Unique Identifier | 5 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
| Unique Identifier | 6 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
| Unique Identifier | 7 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
每个路由键都必须包含字母和两位数字,但一种特定情况除外,即 D6W
代码。
所以以A5W
、C6W
、V0W
开头的代码是无效的。
根据章节 1.5.1 Recommendations for Storage and Presentation
- Eircode 应始终存储为七个大写字符的单个字符串
IT系统,即A65F4E2。
- Eircode 应始终以大写形式显示,由 space 分隔的两个部分,
在文具、邮件、计算机表格等上,即 A65 F4E2 而不是 A65F4E2。
存储在数据库中的代码不应该用space
或dash
分隔,应该只用space
分隔并且仅用于显示。
假设,正确的正则表达式应该如下所示:
/([AC-FHKNPRTV-Y]\d{2}|D6W)[0-9AC-FHKNPRTV-Y]{4}/
从 hywak answer 开始并遵循其他评论建议,这是我的 php 正则表达式:
/^([AC-FHKNPRTV-Y]\d{2}|D6W)\s[0-9AC-FHKNPRTV-Y]{4}$/
我添加了 ^ 和 $ 来定义字符串的开始和结束。
添加 \s 以考虑 space 并接受格式 XXX XXXX.
关于格式 letter/numbers 和要避免的字母的参考:https://en.wikipedia.org/wiki/List_of_postal_codes
最后通过测试的代码说明如下:
- D14 N2Fz -> 最后一个字母小写
- a65 f4e2 -> 所有字符都是
小写
- D6W FNTO -> 不允许字母 O
我想知道是否有验证爱尔兰 Eircode 格式的最佳实践。到目前为止,我在 JavaScript 中使用 REGEX 的最佳尝试是基于第 11 页上的官方规范 here.
(第 11 页基于文档中的页码,如果包含封面则为第 12 页)
/^[A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y]{1}[0-9]{1}[0-9,W]{1}[\ \-]?[0-9,A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y]{4}$/
我在这里没有找到任何与 Eircode 相关的问题,所以我想我会打开这个问题,看看其他人的想法,看看任何人都能想出什么 better/shorter/more 有效的模式。
编辑:根据@Asunez 的回答删除了逗号。
/^[ACDEFHKNPRTVWXY]{1}[0-9]{1}[0-9W]{1}[\ \-]?[0-9ACDEFHKNPRTVWXY]{4}$/
更新了这个答案,避免了 char B
。你可以试试这个:
/^[AC-Y]{1}[0-9]{1}[0-9W]{1}[ \-]?[0-9AC-Y]{4}$/
描述:
^ assert position at start of the string
[AC-Y]{1} match a single character present in the list below
Quantifier: {1} Exactly 1 time (meaningless quantifier)
A the literal character A (case sensitive)
C-Y a single character in the range between C and Y (case sensitive)
[0-9]{1} match a single character present in the list below
Quantifier: {1} Exactly 1 time (meaningless quantifier)
0-9 a single character in the range between 0 and 9
[0-9W]{1} match a single character present in the list below
Quantifier: {1} Exactly 1 time (meaningless quantifier)
0-9 a single character in the range between 0 and 9
W the literal character W (case sensitive)
[ \-]? match a single character present in the list below
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
the literal character
\- matches the character - literally
[0-9AC-Y]{4} match a single character present in the list below
Quantifier: {4} Exactly 4 times
0-9 a single character in the range between 0 and 9
A the literal character A (case sensitive)
C-Y a single character in the range between C and Y (case sensitive)
$ assert position at end of the string
由于@Manwal 的回答并没有完全做到它应该做的,这里是我尝试缩短 OP 的正则表达式:
(?:^[AC-FHKNPRTV-Y][0-9]{2}|D6W)[ -]?[0-9AC-FHKNPRTV-Y]{4}$
支持 A65 B2CD 邮政编码的更新版本 - (?:^[AC-FHKNPRTV-Y][0-9]{2}|D6W)[ -]?[0-9AC-FHKNPRTV-Y]{4}$
这就是您的正则表达式的基本内容,并进行了一些更改:
- 删除了逗号。您不需要逗号来列出
[]
括号内的项目。 - 在可能的情况下添加范围,并在其中节省一些 space(
C-F
、V-Y
)。在其他地方添加范围没有好处,因为它不会使正则表达式更短。 - 您不需要转义 space。正则表达式中的“”是文字。
- 如果破折号是字符 class 中的最后一个字符(方括号) ,则您也不需要转义破折号
- 正则表达式的第一部分现在位于非捕获组中,以允许或使用第三个位置唯一可能的字母“D6W”大小写。
也可以仅使用后视来处理 D6W
,但这比正则表达式更像是一门艺术。
参见正则表达式演示:here
您还可以将字符 class 反转为 不 包括给定字符,虽然它不会使正则表达式更短,但也值得注意。但是,您需要确保不包含其他字符(如点、逗号)。我通过添加 \W
标记来做到这一点。
你可以试试here
根据产品指南第 1.5.4 章,允许的标志是:
-----------------------------------------------------------------------
| Component | Position | Allowed characters |
-----------------------------------------------------------------------
| Routing Keys | 1 | A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
| Routing Keys | 2 | 0-9 |
-----------------------------------------------------------------------
| Routing Keys | 3 | 0-9 with the exception of W for D6W |
-----------------------------------------------------------------------
| Unique Identifier | 4 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
| Unique Identifier | 5 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
| Unique Identifier | 6 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
| Unique Identifier | 7 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
每个路由键都必须包含字母和两位数字,但一种特定情况除外,即 D6W
代码。
所以以A5W
、C6W
、V0W
开头的代码是无效的。
根据章节 1.5.1 Recommendations for Storage and Presentation
- Eircode 应始终存储为七个大写字符的单个字符串 IT系统,即A65F4E2。
- Eircode 应始终以大写形式显示,由 space 分隔的两个部分, 在文具、邮件、计算机表格等上,即 A65 F4E2 而不是 A65F4E2。
存储在数据库中的代码不应该用space
或dash
分隔,应该只用space
分隔并且仅用于显示。
假设,正确的正则表达式应该如下所示:
/([AC-FHKNPRTV-Y]\d{2}|D6W)[0-9AC-FHKNPRTV-Y]{4}/
从 hywak answer 开始并遵循其他评论建议,这是我的 php 正则表达式:
/^([AC-FHKNPRTV-Y]\d{2}|D6W)\s[0-9AC-FHKNPRTV-Y]{4}$/
我添加了 ^ 和 $ 来定义字符串的开始和结束。 添加 \s 以考虑 space 并接受格式 XXX XXXX.
关于格式 letter/numbers 和要避免的字母的参考:https://en.wikipedia.org/wiki/List_of_postal_codes
最后通过测试的代码说明如下:
- D14 N2Fz -> 最后一个字母小写
- a65 f4e2 -> 所有字符都是 小写
- D6W FNTO -> 不允许字母 O