地址字段验证的正则表达式 [英] Regular expression for address field validation

查看:50
本文介绍了地址字段验证的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个便于地址的正则表达式,例如 21-big walkway 或 21 St.Elizabeth's drive 我想出了以下正则表达式,但我不太热衷于如何合并所有字符(字母数字、空格破折号、句号、撇号)

I am trying to write a regular expression that facilitates an address, example 21-big walk way or 21 St.Elizabeth's drive I came up with the following regular expression but I am not too keen to how to incorporate all the characters (alphanumeric, space dash, full stop, apostrophe)

"regexp=^[A-Za-z-0-99999999'

推荐答案

请参阅有关使用正则表达式进行地址验证的问题的答案:正则表达式街道地址匹配

See the answer to this question on address validating with regex: regex street address match

问题是,街道地址的格式差异很大,很难针对它们进行编码.如果您正在尝试验证地址,则很难根据其格式确定地址是否无效.这将返回以下地址 (253 N. Cherry St.),任何格式相同的内容:

The problem is, street addresses vary so much in formatting that it's hard to code against them. If you are trying to validate addresses, finding if one isn't valid based on its format is mighty hard to do. This would return the following address (253 N. Cherry St. ), anything with its same format:

\d{1,5}\s\w.\s(\b\w*\b\s){1,2}\w*\.

这允许 1-5 位数字表示门牌号码、一个空格、一个字符后跟一个句点(用于 N. 或 S.)、1-2 个单词表示街道名称,并以缩写结尾(如 st. 或rd.).

This allows 1-5 digits for the house number, a space, a character followed by a period (for N. or S.), 1-2 words for the street name, finished with an abbreviation (like st. or rd.).

因为正则表达式用于查看事物是否符合标准或协议(您定义的),您可能不想允许上面提供的地址,尤其是第一个带有破折号的地址,因为它们不是很标准.如果您愿意,您可以修改我上面的代码以允许它们 - 您可以添加

Because regex is used to see if things meet a standard or protocol (which you define), you probably wouldn't want to allow for the addresses provided above, especially the first one with the dash, since they aren't very standard. you can modify my above code to allow for them if you wish--you could add

(-?)

允许破折号但不需要破折号.

to allow for a dash but not require one.

此外,http://rubular.com/ 是学习正则表达式的快速互动方式.试试上面的地址.

In addition, http://rubular.com/ is a quick and interactive way to learn regex. Try it out with the addresses above.

这篇关于地址字段验证的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆