We have updated our Privacy Notice, you can read the updated document here
Mods please check the Moderators Group for an important update on Mod tools. If you do not have access to the group, please PM Niamh. Thanks!

Eircode Regex

  • #2
    Registered Users Posts: 42 ✭✭✭ offmeheadpal


    Eircode is coming this year so someone might find this useful

    Code to verify an eircode:
    /^(A|C|D|E|F|H|K|N|P|R|T|V|W|X|Y)([0-9])([0-9]|W)([0-9]|A|C|D|E|F|H|K|N|P|R|T|V|W|X){4}$/
    



    Demo PHP Code
    
    function TidyEircode($eircode)
    {
    	$eircode = preg_replace("/[^A-Za-z0-9 ]/", '', $eircode); //alphanumeric only 
    	$eircode = strtoupper($eircode); //specs say it has to be uppercase
    	return $eircode;
    }
    
    
    function VerifyEircode($eircode)
    {
    	if (preg_match("/^(A|C|D|E|F|H|K|N|P|R|T|V|W|X|Y)([0-9])([0-9]|W)([0-9]|A|C|D|E|F|H|K|N|P|R|T|V|W|X){4}$/", $eircode)) {
    		return true;
    	}
    	else 
    	{
    		return false;
    	}
    }
    
    
    
    $eircode = "A65F4E2";
    $eircode = TidyEircode($eircode);
    
    
    
    if(VerifyEircode($eircode))
    {
    	echo "Eircode Verfied";
    }
    else
    {
    	echo "Please enter a valid Eircode";
    }
    
    
    
    Tagged:


Comments

  • #2


    So if my maths is right and it should be relatively simple to work out, Eircode gives rise to 2.17 Billion possible address within the 26 Counties. The code snippet is useful as well. Could nearly port it to Java or C# quickly.


  • #2


    How do you get 2.17 Billion. It is 15*10*10*25*25*25*25 plus the exception of D6W which gives 586,328,125

    From the Eircode specification
    The Routing Key:

    Begins with an allowed letter
    Followed by two digits

    The only exception is 'D6W' which is a valid Routing Key.

    The Unique Identifier:

    Contains four allowed characters

    An allowed character is either an allowed letter or a digit.

    An allowed letter is an alphabetic character, excluding:

    B, G, I, J, L, M, O, Q, S, U, Z

    A digit is any of 0 to 9.


  • #2


    Ah, the identifiers completely eluded me. I was under the assumption that each character had 36 possibilities. Makes more sense with routing keys as you've stated FSL.


  • #2


    Does that regex not include W as the last of digit of the three digit part for each of the first two digits not just D6?


  • #2


    Very handy. I think you're missing 'Y' out of the 4 character part. This is some code for the exact same task I wrote a couple of weeks back - might be of use to someone.
    <?php
    function eircode_filter($eircode)
    {
       /*
       TRIM, UPPERCASE AND FILTER FOR UNUSED CHARACTERS
       */
       return preg_replace('/[^A-Z0-9 ]/','',strtoupper(trim($eircode)));	
    }
    
    function eircode_validate($eircode)
    {
       /*
       POSITION:
          1 = A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y
          2 = 0-9
          3 = 0-9 with the exception of W for D6W
          [OPTIONAL SPACE]
          4 - 7 = 0-9 or A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y 
       */
       return preg_match('/^(A|[C-F]|H|K|N|P|R|T|[V-Y])([0-9])([0-9]|W)( )?([0-9]|A|[C-F]|H|K|N|P|R|T|[V-Y]){4}$/',$eircode);	
    }
    
    function eircode_format_storage($eircode)
    {
       /* 
       STRIP OUT OPTIONAL SPACE, UPPERCASE, CONFIRM LENGTH = 7
       */
       $eircode = preg_replace('/[^A-Z0-9]/','',strtoupper($eircode));
       return strlen($eircode) == 7 ? $eircode : false;
    }
    
    function eircode_format_display($eircode)
    {
       /* 
       STRIP OUT OPTIONAL SPACE, UPPERCASE, CONFIRM LENGTH = 8 (3 + [SPACE] + 4)
       */	
       $eircode = preg_replace('/[^A-Z0-9]/','',strtoupper($eircode));
       $eircode = substr($eircode,0,3).' '.substr($eircode,3);
       return strlen($eircode) == 8 ? $eircode : false;
    }
    
    
    $eircodes = array(
    'D08   Kf49c',
    'D08 f49c',
    'D08f49c',
    'D08F49C',
    'D08F49B',
    'D6WF49C',
    'D6XF49C'
    );
    
    echo '<pre>';
    foreach($eircodes as $eircode)
    {
    	echo "Testing [".$eircode."]\r\n";
    	
    	// FILTER USER INPUT AND TEST
    	if(eircode_validate(eircode_filter($eircode)))
    	{
    	   echo " - Valid Eircode\r\n";
    	   echo ' - Value for user: '.eircode_format_display($eircode)."\r\n";	
    	   echo ' - Value for database: '.eircode_format_storage($eircode)."\r\n";
    	}
    	else
    	{
    	   echo " - Invalid Eircode\r\n";	
    	}
    	
    	echo "\r\n";
    }
    echo '</pre>';
    ?>
    


  • #2


    rekhib wrote: »
    Very handy. I think you're missing 'Y' out of the 4 character part. This is some code for the exact same task I wrote a couple of weeks back - might be of use to someone.



    Ah yes I forgot the Y in the last part, ill try to edit that. Im still trying to find a bunch of codes to use for testing it out properly


  • #2


    Ah yes I forgot the Y in the last part, ill try to edit that. Im still trying to find a bunch of codes to use for testing it out properly

    If you do get your hands on a list of codes, would you mind posting the link here? That would be very useful.


  • #2


    Rather than
    (A|[C-F]|H|K|N|P|R|T|[V-Y])([0-9])([0-9]|W)
    
    for the routing code, wouldn't
    (((A|[C-F]|H|K|N|P|R|T|[V-Y])[0-9][0-9])|D6W)
    
    be more accurate? The W is only ever allowed in the context of "D6W".


  • #2


    As a personal preference, I would also prefer to use one character class instead of specifying OR repetitively.

    I'd probably do something like this:
    ^(D6W|[AC-FHKNPRTV-Y][0-9]{2})([AC-FHKNPRTV-Y0-9]{4})$
    


  • #2


    I've just started a project where we'll need to use an Eircode validation routine. This thread was the first result I got after a google search. I've come across a few others since that use basically the same sort of regex as provided by the OP.

    However having looked into it a little more, it seems there are only 139 valid routing keys so these regex expressions seem to allow a huge number of invalid routing keys. I've resorted to this javascript regex...

    ^(A41|A42|A45|A63|A67|A75|A81|A82|A83|A84|A85|A86|A91|A92|A94|A96|A98|C15|D01|D02|D03|D04|D05|D06|D07|D08|D09|D10|D11|D12|D13|D14|D15|D16|D17|D18|D20|D22|D24|D6W|E21|E25|E32|E34|E41|E45|E53|E91|F12|F23|F26|F28|F31|F35|F42|F45|F52|F56|F91|F92|F93|F94|H12|H14|H16|H18|H23|H53|H54|H62|H65|H71|H91|K32|K34|K36|K45|K56|K67|K78|N37|N39|N41|N91|P12|P14|P17|P24|P25|P31|P32|P36|P43|P47|P51|P56|P61|P67|P72|P75|P81|P85|R14|R21|R32|R35|R42|R45|R51|R56|R93|R95|T12|T23|T34|T45|T56|V14|V15|V23|V31|V35|V42|V92|V93|V94|V95|W12|W23|W34|W91|X35|X42|X91|Y14|Y21|Y25|Y34|Y35){1}[(A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y,0-9]{4}$


  • #2


    Or. perhaps more compact:
    ^(A(4[125])|(6[37])|(75)|(8[1-6])|(9[12468]))|(C15)|(D(0[1-9])|(1[0-8])|(2[024)|(6W))|(E(2[15])|(3[24])|(4[15])|(53)|(91))|(F(12)|(2[368])|(3[15])|(4[25])|(5[26])|(9[1-4]))|(H(1[2468])|(23)|(5[34])|(6[25])|([79]1))|(K(3[246])|(45)|(56)|(67)|(78))|(N(3[79])|([49]1))|(P(1[247])|(2[45])|(3[126])|(4[37])|(5[16])|(6[17])|(7[25])|(8[15]))|(R(14)|(21)|([34][25])|(5[16])|(9[35]))|(T(12)|(23)|(34)|(45)|(56))|(V(1[45])|(23)|(3[15])|(42)|(9[2-5]))|(W(12)|(23)|(34)|(91))|(X(35)|(42)|(91))|(Y(14)|(2[15])|(3[45]))[ ]*[A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y,0-9]{4}$
    


  • #2


    i hope all your implementations allow eircode to be left blank in the form! google wallet requires a valid eircode in the postcode field when adding a credit card now


  • #2


    google wallet requires a valid eircode in the postcode field when adding a credit card now
    Had a look at my wallet and when adding card doesn't even have field for postcode :confused:


  • #2


    Had a look at my wallet and when adding card doesn't even have field for postcode :confused:

    sorry, i meant google wallet merchant.


  • #2


    bpmurray wrote: »
    Or. perhaps more compact:
    ^(A(4[125])|(6[37])|(75)|(8[1-6])|(9[12468]))|(C15)|(D(0[1-9])|(1[0-8])|(2[024)|(6W))|(E(2[15])|(3[24])|(4[15])|(53)|(91))|(F(12)|(2[368])|(3[15])|(4[25])|(5[26])|(9[1-4]))|(H(1[2468])|(23)|(5[34])|(6[25])|([79]1))|(K(3[246])|(45)|(56)|(67)|(78))|(N(3[79])|([49]1))|(P(1[247])|(2[45])|(3[126])|(4[37])|(5[16])|(6[17])|(7[25])|(8[15]))|(R(14)|(21)|([34][25])|(5[16])|(9[35]))|(T(12)|(23)|(34)|(45)|(56))|(V(1[45])|(23)|(3[15])|(42)|(9[2-5]))|(W(12)|(23)|(34)|(91))|(X(35)|(42)|(91))|(Y(14)|(2[15])|(3[45]))[ ]*[A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y,0-9]{4}$
    

    Thanks for that. Shortening has proved very helpful. My version was ok in javascript but too long to use in oracle. This version is still too long but by validating the routing key and unique identifier seperately, your version brings the routing key regex just under Oracle's limit of 512 characters.


  • #2


    bpmurray wrote: »
    Or. perhaps more compact:
    ^(A(4[125])|(6[37])|(75)|(8[1-6])|(9[12468]))|(C15)|(D(0[1-9])|(1[0-8])|(2[024)|(6W))|(E(2[15])|(3[24])|(4[15])|(53)|(91))|(F(12)|(2[368])|(3[15])|(4[25])|(5[26])|(9[1-4]))|(H(1[2468])|(23)|(5[34])|(6[25])|([79]1))|(K(3[246])|(45)|(56)|(67)|(78))|(N(3[79])|([49]1))|(P(1[247])|(2[45])|(3[126])|(4[37])|(5[16])|(6[17])|(7[25])|(8[15]))|(R(14)|(21)|([34][25])|(5[16])|(9[35]))|(T(12)|(23)|(34)|(45)|(56))|(V(1[45])|(23)|(3[15])|(42)|(9[2-5]))|(W(12)|(23)|(34)|(91))|(X(35)|(42)|(91))|(Y(14)|(2[15])|(3[45]))[ ]*[A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y,0-9]{4}$
    

    Had to revisit this as it rejected a lot of valid eircodes when we tested. It seems to be an issue with parentheses. Nonetheless, using your approach I've come up with a much shorter version.
    ^(A(4[125]|6[37]|75|8[123456]|9[12468])|C15|D(0[1-9]|1[0-8]|2[024]|6W)|(E(2[15]|3[24]|4[15]|53|91))|F(12|2[368]|3[15]|4[25]|5[26]|9[1-4])|H(1[2468]|23|5[34]|6[25]|[79]1)|K(3[246]|45|56|67|78)|N(3[79]|[49]1)|P(1[247]|2[45]|3[126]|4[37]|5[16]|6[17]|7[25]|8[15])|R(14|21|[34][25]|5[16]|9[35])|T(12|23|34|45|56)|V(1[45]|23|3[15]|42|9[2-5])|W(12|23|34|91)|X(35|42|91))[ ]*[(A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y,0-9]{4}$
    


Society & Culture