-1

I'm trying to validate a postcode in the format 'LLNN NLL'. L for letter and N for number. I think I have to use re.match () but I'm not entirely sure. Thanks

1 Answers1

0

Assuming you're talking about UK, try this library, which is designed to deal with UK Postcodes: https://pypi.python.org/pypi/uk-postcode-utils

Alternatively, if you're interested in learning about using regex to solve this problem, you could start with something very simple like

>>> import re
>>> pat = re.compile("[A-Z][A-Z]\d\d \d[A-Z]")
>>> pat.match("AB12 3DE")
<_sre.SRE_Match object at 0x1088b1a58>

and improve it as needed to meet the precise specs you're interested in (handle lowercase? are there disallowed patterns?)

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38