使用 java 将英国邮政编码分为两个主要部分

发布于 2024-10-31 10:24:24 字数 291 浏览 1 评论 0原文

这个用于验证邮政编码的正则表达式工作得很好,

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$

但我想分割邮政编码以使用 java 检索邮政编码的各个部分。

这在java中如何实现呢?

This regular expression for validating postcodes works perfect

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$

but I want to split the postcodes to retrieve the individual parts of the postcode using java.

How can this be done in java?

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

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

发布评论

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

评论(3

剧终人散尽 2024-11-07 10:24:24

以下是匹配英国邮政编码的官方正则表达式:

http://interim.cabinetoffice.gov.uk/media/291370/bs7666-v2-0-xsd-PostCodeType.htm

如果您想将找到的邮政编码分成两部分,这不就是一个简单的空格分割问题吗?英国邮政编码的两部分只是用空格分隔,对吗?在 java 中,这将是:

String[] fields = postcode.split("\\s");

其中邮政编码是经过验证的邮政编码,而 fields[] 将是包含第一部分和第二部分的长度为 2 的数组。

编辑:如果这是为了验证用户输入,并且您想验证第一部分,您的正则表达式将为:

Pattern firstPart = Pattern.compile("[A-Z]{1,2}[0-9R][0-9A-Z]?");

要验证第二部分,它是:

Pattern secondPart = Pattern.compile("[0-9][A-Z-[CIKMOV]]{2}");

Here are the official regexes for matching UK postcodes:

http://interim.cabinetoffice.gov.uk/media/291370/bs7666-v2-0-xsd-PostCodeType.htm

If you want to split a found postcode into it's two parts, isn't it simply a question of splitting on whitespace? A UK postcode's two parts are just separated by a space, right? In java this would be:

String[] fields = postcode.split("\\s");

where postcode is a validated postcode and fields[] will be an array of length 2 containing the first and second parts.

Edit: If this is to validate user input, and you want to validate the first part, your regex would be:

Pattern firstPart = Pattern.compile("[A-Z]{1,2}[0-9R][0-9A-Z]?");

To validate the second part it is:

Pattern secondPart = Pattern.compile("[0-9][A-Z-[CIKMOV]]{2}");
一腔孤↑勇 2024-11-07 10:24:24

我意识到自从提出这个问题以来已经有很长一段时间了,但我有同样的要求,并认为我会发布我的解决方案,以防它对那里的人有帮助:

const string matchString = @"^(?<Primary>([A-Z]{1,2}[0-9]{1,2}[A-Z]?))(?<Secondary>([0-9]{1}[A-Z]{2}))$";

var regEx = new Regex(matchString);
var match = regEx.Match(Postcode);

var postcodePrimary = match.Groups["Primary"];
var postcodeSecondary = match.Groups["Secondary"];

这不会验证邮政编码,但它确实会分割它分成 2 部分(如果它们之间没有输入空格)。

I realise that it's rather a long time since this question was asked, but I had the same requirement and thought that I'd post my solution in case it helps someone out there :

const string matchString = @"^(?<Primary>([A-Z]{1,2}[0-9]{1,2}[A-Z]?))(?<Secondary>([0-9]{1}[A-Z]{2}))$";

var regEx = new Regex(matchString);
var match = regEx.Match(Postcode);

var postcodePrimary = match.Groups["Primary"];
var postcodeSecondary = match.Groups["Secondary"];

This doesn't validate the postcode, but it does split it into 2 parts if no space has been entered between them.

银河中√捞星星 2024-11-07 10:24:24

您可以使用 Google 最近开源的库来实现此目的。 http://code.google.com/p/libphonenumber/

You can use the Google's recentlly open sourced library for this. http://code.google.com/p/libphonenumber/

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