2015-10-28 60 views
8

我想知道是否有愛爾蘭Eircode格式驗證的最佳做法。迄今爲止,在JavaScript中使用REGEX的最佳嘗試如下:基於第11頁上的官方規範here愛爾蘭Eircode驗證

(第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相關的問題,所以我想」打開這一個,看看其他人的想法,看看有什麼更好/更短/更有效的模式,任何人都可以想出來。

編輯:根據@Asunez答案刪除逗號。

/^[ACDEFHKNPRTVWXY]{1}[0-9]{1}[0-9W]{1}[\ \-]?[0-9ACDEFHKNPRTVWXY]{4}$/ 
+0

你能提供例如'愛爾蘭Eircode'的? – Manwal

+0

https://www.eircode.ie/docs/default-source/Common/prepareyourbusinessforeircode-edition3published.pdf?sfvrsn=2 – ConorLuddy

回答

7

自@ Manwal的答案並不完全做自己應該,這是我在縮短的正則表達式OP嘗試:

^[AC-FHKNPRTV-Y]{1}[0-9]{1}[0-9W]{1}[ \-]?[0-9AC-FHKNPRTV-Y]{4}$

這基本上就是你的正則表達式,有一些變化:

  • 刪除逗號。您不需要逗號來列出[]括號內的項目。
  • 在可能的情況下增加範圍並節省一些空間(C-F,V-Y)。在其他地方,添加範圍並不會有好處,因爲它不會縮短正則表達式的範圍。
  • 你不需要逃離一個空間。 「」在正則表達式中是字面的。

它也可能與D6W專門處理向後看,但這是一種藝術,而不是正則表達式。

見正則表達式演示:here

您也可以反轉字符類包括給予人物,雖然它不會使正則表達式更短,這也是值得注意的。但是,您需要確保不包括其他字符(如點,逗號)。我通過添加\W令牌來實現。

,您可以嘗試here

+0

您可以通過'^ [AC -FHKNPRTV-Y] \ d [0-9W] [ - ]?[0-9AC-FHKNPRTV-Y] {4} $' –

+0

@AlexeyShein不一定 - '\ d'和'[0- 9]',第一個也接受希伯來文或其他語言的數字,而第二個只接受0-9。 – Asunez

+0

謝謝@Asunez,關於逗號的好處。 – ConorLuddy

4

更新時間此回答迴避字符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 
+0

嗯,這不正是OPs正則表達式所做的。請注意,例如,您將在開始時接受B代碼。 – Asunez

+0

沒問題,但它不僅在Eircode中不存在'B'。另外你的正則表達式(即使在編輯之後)也會接受'B's,因爲你的範圍是*從*'B' *到*'Y'。 – Asunez

+0

Ohhhhhh上帝完全錯過了。再次更新答案,謝謝。 @Asunez – Manwal

4

根據產品指南章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始終應以大寫形式顯示爲由空格分開的兩部分,在固定式,郵件項目,計算機形式等上的 ,即A65 F4E2和從不A65F4E2。存儲在數據庫

代碼不應與spacedash分離,應該分開,而是僅由space且僅用於顯示。

假設,正確的正則表達式應該是這樣的:

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

Regex online tester

Ericode guide

相關問題