vb.net 加拿大邮政编码正则表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16614648/
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-09-17 13:37:24  来源:igfitidea点击:

Canadian postal code regex?

.netregexvb.net

提问by software

In the postal code field only the following format should be valid.

在邮政编码字段中,只有以下格式才有效。

B1C 2B3 or B1C3D3

B1C 2B3 或 B1C3D3

how to write a regex for this?

如何为此编写正则表达式?

Edited:

编辑:

^([a-zA-Z]\d[a-zA-z]( )?\d[a-zA-Z]\d)$

This is my regex but it only accepting B1C C1B (notice space in between ) format. even with out space should be valid

这是我的正则表达式,但它只接受 B1C C1B(注意中间的空格)格式。即使没有空间也应该有效

回答by Mike Perrenoud

There are some real inconsistencies here. The Regex you provided ^([a-zA-Z]\d[a-zA-z]( )?\d[a-zA-Z]\d)$matches what was stated by Scott regarding the correct Canadian format. However, the examples you provided do not follow the format B1C C1B or B1CC1B.

这里有一些真正的不一致。您提供的正则表达式^([a-zA-Z]\d[a-zA-z]( )?\d[a-zA-Z]\d)$与 Scott 关于正确加拿大格式的说明相符。但是,您提供的示例不遵循格式B1C C1B or B1CC1B

To add insult to injury, the Regex you provided works with the proper Canadian format. So there isn't any real reason to change it. I mean, I would change it to this ^([a-zA-Z]\d[a-zA-Z]\s?\d[a-zA-Z]\d)$so that the single space isn't grouped, but that's just me.

雪上加霜的是,您提供的正则表达式适用于正确的加拿大格式。所以没有任何真正的理由去改变它。我的意思是,我会把它改成这样,这样^([a-zA-Z]\d[a-zA-Z]\s?\d[a-zA-Z]\d)$单个空间就不会被分组,但这只是我。

However, as far as using it, it could be used in C# like this:

但是,就使用而言,它可以像这样在 C# 中使用:

var matches = Regex.Match(inputString, @"^([a-zA-Z]\d[a-zA-Z]( )?\d[a-zA-Z]\d)$");
if (!matches.Success) {
    // do something because it didn't match
}

and now that it's been tagged with VB.NET:

现在它已被标记为 VB.NET:

Dim matches = Regex.Match(inputString, "^([a-zA-Z]\d[a-zA-Z]( )?\d[a-zA-Z]\d)$")
If Not matches.Success Then
    ' do something because it didn't match
End If

回答by Neolisk

You'd want to validate the postal code against the address database. Not every postal code in the format A0A0A0is a valid Canadian postal code. Examples of postal codes that do not exist:

您想根据地址数据库验证邮政编码。并非格式A0A0A0中的每个邮政编码都是有效的加拿大邮政编码。不存在的邮政编码示例:

Z0Z0Z0
Z9Z9Z9
Y7Y7Y7

Regarding preliminary checking, the easiest would probably be one with pre-processing of the value by VB.NET code. You need to remove spaces and convert to upper case. Then your regex is very simple: ([A-Z]\d){3}. And here is the full code for testing:

关于初步检查,最简单的方法可能是通过 VB.NET 代码对值进行预处理。您需要删除空格并转换为大写。那么你的正则表达式是非常简单的:([A-Z]\d){3}。这是用于测试的完整代码:

Imports System.Text.RegularExpressions

Module Module1
  Sub Main()
    Console.WriteLine(CanBeValidCanadianPostalCode("B1C 2B3")) 'prints True
    Console.WriteLine(CanBeValidCanadianPostalCode("B1C3D3")) 'prints True
  End Sub

  Private Function CanBeValidCanadianPostalCode(postal_code As String) As Boolean
    Return Regex.IsMatch(postal_code.Replace(" ", "").ToUpper, "([A-Z]\d){3}")
  End Function
End Module

回答by Arun Kumar S K

Use the below regex for Canadian ZIP code validations

使用以下正则表达式进行加拿大邮政编码验证

^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$