Validation for Irish Eircode

牧云@^-^@ 提交于 2019-12-10 12:39:47

问题


I'm wondering if there's a best practice for validation for the Irish Eircode format. My best attempt so far, using REGEX in JavaScript, is the following based on the official spec found on page 11 here.

(Page 11 based on the page numbers in the document, or page 12 if you include the cover)

/^[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}$/

I didn't find any Eircode related questions on here so I thought I'd open up this one and see what other people thought, and to see what better/shorter/more efficient patterns anyone could come up with.

Edit: Removed commas as per @Asunez answer.

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

回答1:


Since @Manwal's answer doesn't exactly do what it should, here is my attempt at shortening the regex for OP:

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

This is basically what your Regex is, with a few changes:

  • Removed commas. You do not need commas to list items inside [] brackets.
  • Added ranges where possible and where it would save some space (C-F, V-Y). Elsewhere it's not beneficial to add ranges, as it won't make regex shorter.
  • You do not need to escape a space. " " in regex is literal.
  • You also do not need to escape the dash if it's the last character in character class (square brackets)
  • The first part of the regex is now in a non-capturing group to allow ORing it with the only possible letter for 3rd position, the "D6W" case.

It is also possible to deal with D6W exclusively with lookbehind, but this is more of an art than regex.

See Regex Demo: here

You can also invert the character class to not include given characters, and while it doesn't make the regex shorter, it's also worth noting. However, you need to make sure that other characters (like dots, commas) are not included too. I do it by adding the \W token.

You can try it here




回答2:


According to Product guide chapter 1.5.4 allowed signs are:

-----------------------------------------------------------------------
|     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   | 
-----------------------------------------------------------------------

Every routing key must contain letter and two digits except ONE specific situation which is D6W code.

So codes begening with A5W, C6W, V0W are invalid.

According to chapter 1.5.1 Recommendations for Storage and Presentation

  • An Eircode should always be stored as a single string of seven upper case characters in IT systems, i.e. A65F4E2.
  • An Eircode should always be presented in upper case as two parts separated by a space, on stationary, mail items, computer forms, etc. i.e. A65 F4E2 and never A65F4E2.

Codes stored in database shouldn't be separated with space or dash, should be separated but only by space and only for displaying.

Assuming, correct regex should looks like:

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

Regex online tester

Ericode guide




回答3:


Updated this answer avoiding char B. You can try this:

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

Description:

^ 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


来源:https://stackoverflow.com/questions/33391412/validation-for-irish-eircode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!