使用 java 将英国邮政编码分为两个主要部分
这个用于验证邮政编码的正则表达式工作得很好,
^([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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是匹配英国邮政编码的官方正则表达式:
http://interim.cabinetoffice.gov.uk/media/291370/bs7666-v2-0-xsd-PostCodeType.htm
如果您想将找到的邮政编码分成两部分,这不就是一个简单的空格分割问题吗?英国邮政编码的两部分只是用空格分隔,对吗?在 java 中,这将是:
其中邮政编码是经过验证的邮政编码,而 fields[] 将是包含第一部分和第二部分的长度为 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:
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:
To validate the second part it is:
我意识到自从提出这个问题以来已经有很长一段时间了,但我有同样的要求,并认为我会发布我的解决方案,以防它对那里的人有帮助:
这不会验证邮政编码,但它确实会分割它分成 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 :
This doesn't validate the postcode, but it does split it into 2 parts if no space has been entered between them.
您可以使用 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/