正则表达式从地址字符串中提取英国邮政编码的一部分

发布于 2024-12-23 02:12:26 字数 861 浏览 2 评论 0原文

我真的对正则表达式了解不够,无法自己解决这个问题,所以...

我正在使用地理定位 API,我的目标是大致了解访客的行踪,以便我可以列出该特定中提供的服务区域。

这是通过邮政编码字符串的前导部分完成的。例如n17 9ht,我的兴趣是n17

这很公平,但我在chrome和IE9中得到不同的输出,firefox要好得多,并且会自动返回postalCode字符串。

  • chrome: 2 Castlebar Park, London, Greater London W5 1BX, UK
  • ie9: 3-6 Mt Ave, London Borough of Ealing, London W5, UK

而这些都不是我的地址W5是正确的。

问题是,如何从给定的两个可能的字符串中提取 W5 ?

显然,该字符串被 , 分割,因此我正在考虑从如下字符串中获取 W5(或任何可能的英国邮政编码前导部分):

  • Greater London W5 1BX
  • London W5

邮政编码的两部分之间始终有一个空格(如果提供了第二部分)。

我不关心 BFPO / GIR 案例,所以它几乎只是第 1 部分,可以像 n1ec3a 一样简单 - 所以我认为,逻辑需要是:

  1. 提取完整或部分邮政编码。
  2. 获取第 1 部分(或唯一部分)并将其传递下去。

任何帮助表示赞赏。

I really don't know enough about regex to tackle this on my own so...

I am playing with geolocation API and my goal is to get a rough idea about the whereabouts of the visitor so I can list services on offer in that particular area.

this is done via the leading part of the postcode string. eg n17 9ht, my interest is in n17

that's all fair but I get different outputs in chrome and IE9, firefox is miles better and returns a postalCode string automatically.

  • chrome: 2 Castlebar Park, London, Greater London W5 1BX, UK
  • ie9: 3-6 Mt Ave, London Borough of Ealing, London W5, UK

whereas neither of these is my address, W5 is correct.

the question is, how do I extract just W5 out of the two possible strings i am being given?

Obviously, the string is being split by ,, so I am looking at getting W5 (or any possible uk postcode leading part) from a string looking like:

  • Greater London W5 1BX
  • London W5

it will always have a space between the 2 parts of the post code (if 2-nd is supplied).

I don't care about BFPO / GIR cases so it's pretty much just part 1, which can be as simple as n1 or ec3a - so the way I figure, the logic needs to be:

  1. extract either the full or partial postcode.
  2. take part 1 (or the only part) and pass it on.

any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我ぃ本無心為│何有愛 2024-12-30 02:12:26

这是最长的正则表达式之一,根据 支持所有英国邮政编码语法维基百科文章(包括部分内容):

([A-Z]?\d(:? \d[A-Z]{2})?|[A-Z]\d{2}(:? \d[A-Z]{2})?|[A-Z]{2}\d(:? \d[A-Z]{2})?|[A-Z]{2}\d{2}(:? \d[A-Z]{2})?|[A-Z]\d[A-Z](:? \d[A-Z]{2})?|[A-Z]{2}\d[A-Z](:? \d[A-Z]{2})?),\s*UK$

我没有正确测试它,但至少适用于你的情况。

Here is the one of the longest regular expression which supports all UK postal code syntax according to Wikipedia article (including partials):

([A-Z]?\d(:? \d[A-Z]{2})?|[A-Z]\d{2}(:? \d[A-Z]{2})?|[A-Z]{2}\d(:? \d[A-Z]{2})?|[A-Z]{2}\d{2}(:? \d[A-Z]{2})?|[A-Z]\d[A-Z](:? \d[A-Z]{2})?|[A-Z]{2}\d[A-Z](:? \d[A-Z]{2})?),\s*UK$

I didn't test it properly, but at least works for your cases.

鼻尖触碰 2024-12-30 02:12:26

我不熟悉英国邮政编码,但您似乎是在说,已经删除了地址的其他部分,您已将地址范围缩小到:

[Some words] [several-character code] [optional three-character code]

并且您想要两个字符的代码。如果这是正确的,那么可能是这样的正则表达式:

/\s([^\s]{2,4})(?:\s...)?$/

然后您想要的位将由 .match():(

var addressPart = "Greater London W5 1BX",

alert(addressPart.match(/\s([^\s]{2,4})(?:\s...)?$/)[1]); // "W5"

当然,您可以实际测试 .match() 的返回以确保它有效,但是如果它能达到你想要的效果将是匹配数组的第二个元素。)

Dodgy 演示: http://jsfiddle.net/HjD7w/1/编辑

:我怀疑这不太管用,因为在某些情况下,城市名称的最后一个单词可能会被误认为是邮政编码的第一部分,但是如果有关于字母和数字字符如何匹配的固定规则在邮政编码你可以修复它。 (虽然我不知道规则。)

I'm not familiar with UK postcodes, but you seem to be saying that having already removed the other parts of the address you've narrowed the address down to:

[Some words] [several-character code] [optional three-character code]

And you want the two-character code. If that is correct then maybe a regex something like this:

/\s([^\s]{2,4})(?:\s...)?$/

Then the bit you want will be returned by .match():

var addressPart = "Greater London W5 1BX",

alert(addressPart.match(/\s([^\s]{2,4})(?:\s...)?$/)[1]); // "W5"

(Of course you can actually test the return from .match() to be sure it worked, but if it worked the bit you want will be the second element of the match array.)

Dodgy demo: http://jsfiddle.net/HjD7w/1/

EDIT: I suspect this won't quite work because the last word of the city name might be mistaken for the first part of the postcode in some cases, but if there are fixed rules about how the alpha and digit characters fit in the postcode you could fix it. (I don't know the rules though.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文