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
Asked
Active
Viewed 1,462 times
-1
-
1Please update your question, show what you have tried or any testcases anything instead of raw text. – bhansa Sep 25 '17 at 16:32
-
For which country? – Alexander Sep 25 '17 at 16:35
1 Answers
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