如何通过前缀正确匹配英国邮政编码? [英] How to correctly match UK postcodes by prefix?

查看:225
本文介绍了如何通过前缀正确匹配英国邮政编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多餐馆,它们都运送到伦敦的某些邮政编码区域,例如:

I have a number of restaurants who all deliver to certain postcode areas in London, for example:

  • EC1
  • WC1
  • WC2
  • W1
  • EC1
  • WC1
  • WC2
  • W1

当某人搜索要送回家的餐厅时,他们会输入完整的邮政编码.

When someone searches for a restaurant that delivers to their home, they enter their full postcode.

有些人用空格正确输入邮政编码,有些人只输入所有字母和数字,没有空格.为了使事物协调一致,我尝试进行匹配之前先删除邮政编码中的所有空格.

Some people enter the postcode correctly with the space, some of them just enter all letters and numbers attached, without a space separator. To harmonize things, I remove any space in the postcode before attempting a match.

到目前为止,我曾经通过仅检查邮政编码是否以所讨论的前缀开头来将邮政编码与前缀进行匹配,但是后来我意识到这并不是万无一失的:

So far, I used to match the postcode to the prefixes by just checking if it starts with the prefix in question, but then I realized that this is not foolproof:

  • WC1E123 =>正确匹配WC1
  • W1ABC =>正确匹配W1
  • W10ABC =>与W1的匹配不正确,应仅匹配W10前缀
  • WC1E123 => correct match for WC1
  • W1ABC => correct match for W1
  • W10ABC => incorrect match for W1, should only match the W10 prefix

如果匹配给定前缀,同时又没有通过上面的W1/W10测试,我怎么知道给出了没有空格的完整邮政编码? ?

How can I know, given a full postcode with no space, if it matches a given prefix, while not failing the W1 / W10 test above?

该问题是否有任何解决方案,而不会涉及强迫客户输入邮政编码并在正确位置放置空格?

Is there any solution at all to the problem, that would not involve forcing the customer to enter the postcode with the space at the correct position?

推荐答案

在英国,邮政编码有6种可能的格式:

There are 6 possible formats for postcodes in the UK:

  • A9 9AA
  • A9A 9AA
  • A99 9AA
  • AA9 9AA
  • AA9A 9AA
  • AA99 9AA

我认为您的解决方案需要分为两个部分.首先是验证输入;第二个是抓住第一部分.

I think there need to be two parts to your solution. The first is to validate the input; the second is to grab that first part.

验证

这真的很重要,即使我意识到您已经说过这不是您要尝试做的事情,但是如果没有它,您将很难获得正确的前缀并可能将驱动程序发送到错误的位置!

This is really important, even though I realise you have said this is not what you are trying to do, but without it you are going to struggle to get the right prefix and possibly send your drivers to the wrong place!

您可以通过两种方式来做到这一点,或者使用第三方来帮助您获取完整的&正确的地址(包括 http://www.qas.co. uk/knowledge-centre/product-information/address-postcode-finder.htm (我的公司)),或至少使用一些reg-ex/类似的健全性测试来验证邮政编码-例如链接Dmitri给了你以上.

There are a couple of ways you can do it, either use a 3rd party to help you capture a complete & correct address (many available including http://www.qas.co.uk/knowledge-centre/product-information/address-postcode-finder.htm (my company)), or at a minimum use some reg-ex / similar sanity testing to validate the postcodes - such as the links Dmitri gave you above.

如果您查看列出的测试用例-W1ABC和W10ABC都不是有效的邮政编码-如果我们正确地确定了该位,那么下一位就变得容易多了.

If you look at the test cases you have listed - W1ABC and W10ABC are not valid postcodes - if we get that bit correct then the next bit becomes a lot easier.

提取前缀

假设您现在拥有完整,有效的邮政编码,那么只需获取第一部分(输出代码)就会变得更加容易-带有或不带有空格.由于下半部分(incode)的标准格式为 9AA ,即digit-alpha-alpha,因此,我将通过发现并删除它来做到这一点,只剩下您的输出码,无论它是W1还是W1 0AA ,或来自W10 0AA的W10.

Assuming you now have a full, valid postcode getting just the first part (outcode) becomes a lot easier - with or without spaces. Because the second half (incode) has a standard format of 9AA, digit-alpha-alpha, I would do it by spotting and removing this, leaving you with just your outcode whether it be W1 From W1 0AA, or W10 from W10 0AA.

或者,如果您使用第三方来捕获地址,则大多数人将能够分别为您返回incode和outcode.

Alternatively, if you are using a 3rd party to capture the address - most of them will be able to return the incode and outcode separately for you.

这篇关于如何通过前缀正确匹配英国邮政编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆