Python, Canadian Address RegEx validation -
im trying write python script validates canadian addresses using regex.
for example address valid:
" 123 4th street, toronto, ontario, m1a 1a1 " but 1 not valid:
" 56 winding way, thunder bay, ontario, d56 4a3" i have tried many different combinations keeping rules of canadian postal codes such last 6 alphanumeric bits cannot contain letters (d,f,i,o,q,u,w,z) entries seem come out invalid. , tried " ('^[abceghjklmnprstvxy]{1}\d{1}[a-z]{1} *\d{1}[a-z]{1}\d{1}$') " still invalid
this have far
import re postalcode = " 123 4th street, toronto, ontario, m1a 1a1 " #read first line line = postalcode #validation statement test=re.compile('^\d{1}[abceghjklmnprstvxy]{1}\d{1}[a-z]{1} *\d{1}[a-z]{1}\d{1}$') if test.match(line) not none: print 'match found - valid canadian address: ', line else: print 'error - no match - invalid canadian address:', line
canadian postal codes can't contain letters d, f, i, o, q, or u, , cannot start w or z:
this work you:
import re postalcode = " 123 4th street, toronto, ontario, m1a 1a1 " #read first line line = postalcode if re.search("[abceghjklmnprstvxy][0-9][abceghjklmnprstvwxyz] ?[0-9][abceghjklmnprstvwxyz][0-9]", line , re.ignorecase | re.dotall): print 'match found - valid canadian address: ', line else: print 'error - no match - invalid canadian address:', line wrong - 56 winding way, thunder bay, ontario, d56 4a3 correct - 123 4th street, toronto, ontario, m1a 1a1
Comments
Post a Comment