Check for Irish Eircode

I am wondering if there is best practice for checking the Irish Eircode format. My best attempt so far using REGEX in JavaScript is as follows: based on the official specification found on page 11 here .

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

/^[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 questions related to Eircode here, so I decided to open this one and see what other people think and see which better / shorter / more efficient templates they might come up with.

Edit: Deleted commas according to @Asunez answer.

 /^[ACDEFHKNPRTVWXY]{1}[0-9]{1}[0-9W]{1}[\ \-]?[0-9ACDEFHKNPRTVWXY]{4}$/ 
+8
javascript regex street-address
source share
3 answers

Since @Manwal's answer doesn't exactly do what it needs, here is my attempt to shorten the regex for OP:

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

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

  • Deleted commas. You do not need commas to list items inside brackets [] .
  • Added ranges where it is possible and where it will save some space ( CF , VY ). Elsewhere, it is not beneficial to add ranges, as this will not make the regular expression shorter.
  • You do not need to hide the space. "" in regular expression is literal.

It is also possible to deal with D6W solely with lookbehind, but this is more an art than a regular expression.

See Regex Demo: here

You can also invert a character class to not include specified characters, and although it does not make the regular expression shorter, it is also worth noting. However, you need to make sure that other characters (such as periods, commas) are also not included. I do this by adding the \W token.

You can try here

+7
source share

Updated this answer avoiding char B You can try the following:

 /^[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) CY 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) CY a single character in the range between C and Y (case sensitive) $ assert position at end of the string 
+4
source share

The following characters are permitted according to chapter 1.5.4 of the product manual:

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

Each routing key must contain a letter and two digits, except for ONE specific situation, which is the D6W code.

Therefore, codes starting with A5W , C6W , V0W are not valid.

According to chapter 1.5.1 Recommendations for Storage and Presentation

  • Eircode should always be stored as one line of seven uppercase characters in an IT system, i.e. A65F4E2.
  • Eircode should always be represented in uppercase as two parts, separated by a space, to stationary, mail, computer forms, etc., i.e. A65 F4E2 and never A65F4E2.

Codes stored in the database should not be shared by space or dash , should be divided, but only into space and only for display.

Assuming the correct regular expression should look like this:

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

online tester regex

Ericode Guide

+4
source share

All Articles