Skip to content

Instantly share code, notes, and snippets.

@idlefingers
Created July 14, 2010 14:32
Show Gist options
  • Select an option

  • Save idlefingers/475475 to your computer and use it in GitHub Desktop.

Select an option

Save idlefingers/475475 to your computer and use it in GitHub Desktop.
def check_postcode(postcode)
alpha1 = "[abcdefghijklmnoprstuwyz]"
alpha2 = "[abcdefghklmnopqrstuvwxy]"
alpha3 = "[abcdefghjkstuw]"
alpha4 = "[abehmnprvwxy]"
alpha5 = "[abdefghjlnpqrstuwxyz]"
postcode_expressions = [
%r{^(#{alpha1}{1}#{alpha2}?[0-9]{1,2})(\s*)([0-9]{1}#{alpha5}{2})$}, # AN NAA, ANN NAA, AAN NAA, and AANN NAA
%r{^(#{alpha1}{1}[0-9]{1}#{alpha3}{1})(\s*)([0-9]{1}#{alpha5}{2})$}, # ANA NAA
%r{^(#{alpha1}{1}#{alpha2}?[0-9]{1}#{alpha4}{1})(\s*)([0-9]{1}#{alpha5}{2})$} # AANA NAA
]
not postcode_expressions.collect{ |expression| postcode.downcase =~ expression }.compact.empty?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment