验证爱尔兰Eircode

Con*_*ddy 17 javascript regex street-address

我想知道是否有最佳的爱尔兰Eircode格式验证方法.我最好的尝试,到目前为止,在JavaScript中使用正则表达式,是基于第11页上找到的官方规格如下这里.

(第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}$/
Run Code Online (Sandbox Code Playgroud)

我没有在这里找到任何与Eircode相关的问题,所以我想我会打开这个问题,看看其他人的想法,看看有什么更好/更短/更有效的模式,任何人都可以想出来.

编辑:根据@Asunez回答删除逗号.

/^[ACDEFHKNPRTVWXY]{1}[0-9]{1}[0-9W]{1}[\ \-]?[0-9ACDEFHKNPRTVWXY]{4}$/
Run Code Online (Sandbox Code Playgroud)

Asu*_*nez 14

由于@Manwal的回答并不完全符合它应该做的,这是我试图缩短OP的正则表达式:

(?:^[AC-FHKNPRTV-Y][0-9]{2}|D6W)[ -]?[0-9AC-FHKNPRTV-Y]{4}$

这基本上是你的正则表达式,只有一些变化:

  • 删除了逗号.您不需要逗号列出[]括号内的项目.
  • 尽可能添加范围以及节省一些空间(C-F,V-Y)的位置.在其他地方添加范围是不利的,因为它不会使正则表达式更短.
  • 你不需要逃离空间.正则表达式中的""是字面的.
  • 如果它是字符类中的最后一个字符(方括号),则也不需要转义短划线
  • 正则表达式的第一部分现在处于非捕获组,以允许OR与第三位置,"D6W"情况下,唯一可能的字母荷兰国际集团它.

也可以D6W专门处理lookbehind,但这更像是一种艺术而不是正则表达.

请参阅正则表达式演示:此处

您也可以将字符类反转为包含给定字符,虽然它不会使正则表达式更短,但也值得注意.但是,您需要确保不包含其他字符(如点,逗号).我是通过添加\W令牌来实现的.

你可以在这里试试

  • 您的正则表达式有两个问题:它允许破折号(未在 eircode 规范中定义)并接受所有以 W 结尾且无效的路由代码,例如:A1W、C2W。可选空间的大加分 - 未定义但显示在 eircode 示例中,并遵循与英国邮政编码相同的模式。 (2认同)

hyw*_*wak 10

根据产品指南第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   | 
-----------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

每个路由密钥必须包含字母和两个数字,除了一个特定情况,即D6W代码.

因此,与begening代码A5W,C6W,V0W是无效的.

根据章节 1.5.1 Recommendations for Storage and Presentation

  • Eircode应始终存储为IT系统中包含七个大写字符的单个字符串,即A65F4E2.
  • Eircode应始终以大写形式显示为由空格,静止,邮件,计算机表格等分隔的两个部分,即A65 F4E2,而不是A65F4E2.

存储在数据库中的代码不应与space或分开dash,应该分开,但space仅限于显示.

假设,正确的正则表达式应如下所示:

/([AC-FHKNPRTV-Y]\d{2}|D6W)[0-9AC-FHKNPRTV-Y]{4}/

正则表达式在线测试

Ericode指南


Man*_*wal 5

更新了这个答案,避免了 char B。你可以试试这个:

/^[AC-Y]{1}[0-9]{1}[0-9W]{1}[ \-]?[0-9AC-Y]{4}$/
Run Code Online (Sandbox Code Playgroud)

描述:

^ 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
Run Code Online (Sandbox Code Playgroud)