c#英国邮政编码拆分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1172097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:59:30  来源:igfitidea点击:

c# UK postcode splitting

c#string

提问by Tikeb

I need a way to split a UK postcode from user entry. This means the postocode could be nicely formatted full code like so "AB1 1BA" or it could be anything you could imagine. I've seen some regex to check the format of the postcode but it's knowing where to split it if I'm given something like "AB111AD" etc.. This is to return the first part of the postcode, in the example above would be "AB11". Any thoughts? Thanks..

我需要一种从用户输入中拆分英国邮政编码的方法。这意味着邮编可以很好地格式化完整代码,例如“AB1 1BA”,或者它可以是您可以想象的任何内容。我已经看到一些正则表达式来检查邮政编码的格式,但是如果我得到诸如“AB111AD”之类的东西,它知道在哪里拆分它。这是返回邮政编码的第一部分,在上面的例子中是“AB11”。有什么想法吗?谢谢..

采纳答案by Brandon

I'm not sure how UK Post Codes work, so is the last part considered the last 3 characters with the first part being everything before?

我不确定英国邮政编码是如何工作的,所以最后一部分是否被认为是最后 3 个字符,而第一部分是之前的所有字符?

If it is, something like this should work, assuming you've already handled appropriate validation: (Edited thanks to Jon Skeets commment)

如果是这样,假设您已经处理了适当的验证,这样的事情应该可以工作:(感谢 Jon Skeets 评论编辑)

string postCode = "AB111AD".Replace(" ", "");
string firstPart = postCode.Substring(0, postCode.Length - 3);

That will return the Post Code minus the last 3 characters.

这将返回邮政编码减去最后 3 个字符。

回答by Jon Skeet

I've written something similar in the past. I thinkyou can just split before the last digit. (e.g. remove all spaces, find the last digit and then insert a space before it):

我以前写过类似的东西。我认为您可以在最后一位数字之前拆分。(例如删除所有空格,找到最后一位数字,然后在它之前插入一个空格):

static readonly char[] Digits = "0123456789".ToCharArray();

...

string noSpaces = original.Replace(" ", "");
int lastDigit = noSpaces.LastIndexOfAny(Digits);
if (lastDigit == -1)
{
    throw new ArgumentException("No digits!");
}
string normalized = noSpaces.Insert(lastDigit, " ");

The Wikipedia entryhas a lot of detail including regular expressions for validation (after normalisation :)

维基百科条目有很多细节,包括用于验证的正则表达式(正常化后:)

回答by Shoban

I have worked with many UK insurance websites and we normally ask both the parts in different text boxes. How are you validating the address? In some sites we ask the post code together but we use QASto validate the postcode and ask the user to select the address. QAS can validate even if the postcode is entered together.

我曾与许多英国保险网站合作,我们通常会在不同的文本框中询问这两个部分。你是如何验证地址的?在某些站点中,我们一起询问邮政编码,但我们使用QAS来验证邮政编码并要求用户选择地址。即使邮政编码一起输入,QAS 也可以验证。

回答by Boris Modylevsky

Regular expressions may help to easily parse UK post code by using named groups for each part of the code. Regular expressions may be taken from here:

通过对代码的每个部分使用命名组,正则表达式可能有助于轻松解析英国邮政编码。正则表达式可以从这里获取:

http://www.regxlib.com/REDetails.aspx?regexp_id=260

http://www.regxlib.com/REDetails.aspx?regexp_id=260

or here:

或在这里:

http://www.mgbrown.com/PermaLink66.aspx

http://www.mgbrown.com/PermaLink66.aspx



string ukPostCode = "AB1 1BA";
// Add group names in the pattern like this {FIRST_GROUP}    
string UK_POST_PATTERN = @"^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$";

Regex ukPostRegex = new Regex(UK_POST_PATTERN, RegexOptions.Compiled);

Match match = ukPostRegex.Match(ukPostCode);
if (match.Success)
{
    Group group = match.Groups["FIRST_GROUP"];
   // etc
}

回答by silves89

Brandon is correct, you can substring on the last 3 characters because this is the format defined by the standard BS 7666. So "AA1 11AA" isn't in fact a valid postcode.

Brandon 是正确的,您可以在最后 3 个字符上添加子字符串,因为这是标准 BS 7666 定义的格式。因此“AA1 11AA”实际上不是有效的邮政编码。

Apparently the only breach of BS 7666 is British Forces Post Office postcodes, which begin BFPO and end with 4 numbers, e.g. "BFPO 1234".

显然,唯一违反 BS 7666 的是英国军队邮局邮政编码,它以 BFPO 开头并以 4 个数字结尾,例如“BFPO 1234”。

The Wikipedia entryhas more information, as well as regexs for validation. It also has a choice quote: "completely accurate validation is only possible by attempting to deliver mail to the address."

维基百科条目有更多的信息,以及用于验证regexs。它还有一个选择引用:“只有尝试将邮件发送到该地址,才能进行完全准确的验证。”

回答by GlennG

Assuming a valid input string, e.g. one that's passed the following regex:

假设一个有效的输入字符串,例如通过以下正则表达式的字符串:

"^[A-Za-z]{1,2}[0-9Rr][0-9A-Za-z]?\s*?[0-9][ABDEFGHJLNPQRSTUWXYZabdefghjlnpqrstuwxyz]{2}$"

Note: this allows optional space between the Outcode and Incode.

注意:这允许 Outcode 和 Incode 之间的可选空间。

Then the following replacement regex will tidy it up - (splits it into two captures $1 and $2, then inserts the space):

然后下面的替换正则表达式将其整理 - (将其拆分为两个捕获 $1 和 $2,然后插入空格):

postCode = Regex.Replace(postCode, "^(\S+?)\s*?(\d\w\w)$", " ")

回答by Rao

UK-postcodes format explained:

英国邮政编码格式说明:

Ref: http://www.mrs.org.uk/pdf/postcodeformat.pdf

参考:http: //www.mrs.org.uk/pdf/postcodeformat.pdf

POSTCODE FORMAT

邮政编码格式

A Postcode is made up of the following elements: PO1 3AX

邮政编码由以下元素组成: PO1 3AX

  • PO the area. There are 124 postcode areas in the UK
  • 1 the district. There are approximately 20 Postcode districts in an area
  • 3 the sector. There are approximately 3000 addresses in a sector.
  • AX the Unit. There are approximately 15 addresses per unit.
  • PO 区域。英国有 124 个邮政编码区
  • 1区。一个地区大约有 20 个邮政编码区
  • 3 部门。一个扇区中大约有 3000 个地址。
  • AX 单位。每个单元大约有 15 个地址。

The following list shows all valid Postcode formats. "A" indicates an alphabetic character and "N" indicates a numeric character.

以下列表显示了所有有效的邮政编码格式。“A”表示字母字符,“N”表示数字字符。

FORMAT EXAMPLE:

AN NAA - M1 1AA
ANN NAA - M60 1NW
AAN NAA - CR2 6XH
AANN NAA - DN55 1PT
ANA NAA - W1A 1HQ
AANA NAA - EC1A 1BB

Please note the following:

请注意以下事项:

  • The letters Q, V and X are not used in the first position
  • The letters I,J and Z are not used in the second position.
  • The only letters to appear in the third position are A, B, C, D, E, F, G, H, J, K, S, T, U and W.
  • The second half of the postcode is always consistent numeric, alpha, alpha format and the letters C, I, K, M, O and V are never used.
  • 字母 Q、V 和 X 不用于第一个位置
  • 字母 I、J 和 Z 不用于第二个位置。
  • 出现在第三位的唯一字母是 A、B、C、D、E、F、G、H、J、K、S、T、U 和 W。
  • 邮政编码的后半部分始终是一致的数字、字母、字母格式,并且从不使用字母 C、I、K、M、O 和 V。

And it is safe to assume that the spacewill be the forth character from the end, ie., if a postcode is missing a space, SW109RL, you can blindly put a space at the 4th position from the end, SW10 9RL

并且可以安全地假设spacewill 是结尾SW109RL的第四个字符,即,如果邮政编码缺少一个空格,,您可以盲目地在结尾的第 4 个位置添加一个空格,SW10 9RL

回答by JsonStatham

I've written a method to handle this as best I can for my application.

我已经编写了一种方法来尽可能为我的应用程序处理这个问题。

Firstly I put a max length on my textbox @maxlength="8"

首先,我在文本框上设置了最大长度 @maxlength="8"

Then I call the following method in my controller:

然后我在控制器中调用以下方法:

    if (searchString != null)
    {
        searchString = HtmlHelpers.ParseStringToUkPostcode(searchString);
    }      

The method consists of:

该方法包括:

public static string ParseStringToUkPostcode(string inputPostcode)
        {
            string outputPostcode = inputPostcode;

            outputPostcode = outputPostcode.Replace(" ", "").Trim();
            string outward = outputPostcode.Substring(0, outputPostcode.Length - 3);
            string inward = outputPostcode.Substring(Math.Max(0, outputPostcode.Length - 3));
            string postcode = string.Format("{0} {1}", outward, inward);
            outputPostcode = postcode;

            return outputPostcode;
        }

It's not pretty but it seems to be relatively robust for what I need and will handle the obvious variations of user provided post codes, in my area at least.

它并不漂亮,但它似乎对我需要的东西来说相对强大,并且至少在我所在的地区可以处理用户提供的邮政编码的明显变化。

Disclaimer.

免责声明

I don't need to look at postcodes outside my county as far as this app is concerned.

就这个应用程序而言,我不需要查看我县以外的邮政编码。

回答by Matas Vaitkevicius

I wrote this regex based on @Jon Skeet's suggestion that last 3 are always digit-letter-letter

我根据@Jon Skeet 的建议编写了这个正则表达式,即最后 3 个总是数字字母

([a-zA-Z][a-zA-Z0-9]{1,3}) ?(\d[a-zA-Z]{2})

([a-zA-Z][a-zA-Z0-9]{1,3}) ?(\d[a-zA-Z]{2})

I will probably let through some bad ones due to this part [a-zA-Z0-9]{1,3}but hey its good enough for me. You can test it here.

由于这部分,我可能会通过一些不好的部分,[a-zA-Z0-9]{1,3}但嘿,它对我来说已经足够了。你可以在这里测试它

回答by gdmanandamohon

  Simply split the string in equally section or as your requirements it may be single/double/triple bit



string postCode = "AB111AD".Replace(" ", "");
    string firstPart = postCode.Substring(0, postCode.Length - 3);
    string lastPart = postCode.Substring(postCode.Length -3,postCode.Length);