如何使用 .NET Regex.Match 验证 Geonames 国家/地区信息数据库中的邮政/邮政编码正则表达式
您好,
我正在使用 Geonames 数据库的本地副本,包括国家/地区信息数据。我的自定义 .NET 应用程序需要验证用户输入的邮政编码。验证需要在本地完成,并且不能使用可用的 Geonames Web 服务。这是我的申请的要求。我希望你能帮我弄清楚如何做到这一点。
Geonames 国家/地区信息数据库内部有一个字段,其中包含用于验证相应国家/地区的国家/地区/邮政编码的正则表达式。以下是加拿大和美国数据中包含的正则表达式:
Geonames 数据库中的正则表达式:
Canada: ^([a-zA-Z]d[a-zA-Z]d[a-zA-Z]d)$
USA: ^(d{9})$
在我的应用程序中,验证完成后,我首先识别所选的国家/地区,然后查找有效的邮政编码特定国家/地区的正则表达式。然后,我使用以下 .NET/C# 代码来测试匹配:
bool testResult = Regex.IsMatch(postalCode, geonamesRegexForCountry);
当我输入邮政编码的有效示例数据时,正则表达式匹配测试总是失败。以下是一些有效值:
Canada:
Postal Code: L5R3K6
Postal Code: L8M1L5
USA:
Zip Code: 35801
Zip Code: 72201
测试总是失败。知道为什么吗? Geonames 数据库使用的“正则表达式语法”是否与 .NET Regex.Match()
函数使用的不同?关于如何从这里继续进行有什么建议吗?
我需要验证所有国家/地区的邮政编码 - 不仅仅是加拿大和美国,因此我真的希望能够利用 Geonames 数据库中现有的丰富内容!
感谢您的帮助!
瞬间冲浪者
Greetings,
I'm using a local copy of the Geonames database, including the country-info data. My custom .NET application needs to validate the Postal/Zip codes that users input. The validation needs to be done locally and can not use the Geonames web services that are available. This is a requirement of my application. I'm hoping you can help me figure out how to do this.
Inside of the Geonames Country Info database there is a field that includes the regular expression to validate the Country/Zip code for the corresponding country. Here are the regular expressions that are contained in the data for both Canada and the USA:
Regexs in the Geonames database:
Canada: ^([a-zA-Z]d[a-zA-Z]d[a-zA-Z]d)$
USA: ^(d{9})$
In my application, once the validation is done I first identify the country that is selected and then I look up the valid postal code regex for the specific country. I then use the following .NET/C# code to test for a match:
bool testResult = Regex.IsMatch(postalCode, geonamesRegexForCountry);
When I enter valid sample data for the postal/zip codes, the regular expression match test always fails. Here are some valid values:
Canada:
Postal Code: L5R3K6
Postal Code: L8M1L5
USA:
Zip Code: 35801
Zip Code: 72201
The tests are always failing. Any idea why? Does the Geonames database use a different "regular expression syntax" than the .NET Regex.Match()
function uses? Any suggestions on how to proceed from here?
I need to validate the postal/zip codes for all countries - not just Canada and the USA, so I'm really hoping I can leverage the wealth of content existing in the Geonames database!
Thanks for all of your help!
MomentSurfer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这只是一个格式问题,但您的正则表达式中缺少反斜杠:
当然,它们可能与您的实际数据不匹配。例如,美国版本仅匹配
123456789
等邮政编码(正好 9 位数字),但不匹配12345
或12345-6789
代码>.加拿大版本同样不允许字符之间有任何分隔符。Perhaps it's just a formatting problem, but there are backslashes missing in your regexes:
And, of course, they might not be matching your actual data. For example, the USA version only matches Zip codes like
123456789
(exactly 9 digits) but not12345
or12345-6789
. And the Canada version likewise doesn't allow for any separators between the characters.