python的Imrove Regex(2.7)寻找短 /不完整的英国邮政编码
我有一些功能,可以在字符串中找到英国完整的邮政编码(例如DE2 7TT),并相应地返回它们。
但是,我想将其更改为还返回邮政编码,它到达一个或两个字母,然后是一个或两个数字(例如SE3,E2,SE45,E34)。
即,它必须收集两种形式的英国邮政编码(不完整和完整)。
代码是:
def pcsearch(postcode):
if bool(re.search('(?i)[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}', postcode)):
postcode = re.search('(?i)[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}', postcode)
postcode = postcode.group()
return postcode
else:
postcode = "na"
return postcode
要使此调整还需要进行哪些调整,以便与这些较短,不完整的邮政编码一起使用?
I have a little function that finds full UK postcodes (e.g. DE2 7TT) in strings and returns them accordingly.
However, I'd like to change it to ALSO return postcodes it gets where there's either one or two letters and then one or two numbers (e.g. SE3, E2, SE45, E34).
i.e. it must collect BOTH forms of UK postcode (incomplete and complete).
The code is:
def pcsearch(postcode):
if bool(re.search('(?i)[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}', postcode)):
postcode = re.search('(?i)[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}', postcode)
postcode = postcode.group()
return postcode
else:
postcode = "na"
return postcode
What tweaks are needed to get this to ALSO work with those shorter, incomplete, postcodes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用交替和单词边界编写模式。
regex demo
只能通过检查匹配项即可使用该模式重新制定代码:
输出:输出:
You might write the pattern using an alternation and word boundaries.
Regex demo
The code could be refactored using the pattern only once by checking the match:
Output