Created
June 9, 2014 13:57
-
-
Save maniksurtani/7b9b4c567895ad9b346a to your computer and use it in GitHub Desktop.
Address checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
_POSTCODE_RE = re.compile('\s*[a-zA-Z]{1,2}[0-9][0-9a-zA-Z]?\s?[0-9][a-zA-Z]{2}\s*') | |
_SPLIT_POSTCODE_RE = re.compile('(.*)\s*([a-zA-Z]{1,2}[0-9][0-9a-zA-Z]?\s?[0-9][a-zA-Z]{2})\s*') | |
_COORDS_RE = re.compile("^([0-9.-]+),([0-9.-]+)$") | |
def is_coords(address): | |
return _COORDS_RE.match(address) != None | |
def is_postcode(address): | |
return _POSTCODE_RE.match(address) != None | |
def split_address(address): | |
matches = _SPLIT_POSTCODE_RE.match(address) | |
address = None | |
postcode = None | |
if matches: | |
address = m.group(1) | |
postcode = m.group(2) | |
if address: | |
address = address.strip() | |
else: | |
address = None | |
if postcode: | |
postcode = postcode.upper().strip() | |
else: | |
postcode = None | |
return address, postcode | |
## Tests | |
print is_coords("12.345,-1.7011") | |
print is_postcode("N1 8SU") | |
print is_postcode(" w2 4dy") | |
print is_postcode("286, New North Road, London N1 8SU") | |
print split_address("286, New North Road, London N1 8Ssu") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment