希望验证PO,但想知道这种验证是否存在。我将地址字段分割为地址1和地址2( PO、Apt、Suite信息将在其中进行)
示例:
Address 1: 123 Main Street
Address 2: Suite 100
City: Any Town
State: Any State
Zip: Any Zip收件箱(也可以用BIN为方框)示例:
H 110P.O 123H 211H 112 Box
F 230
(我知道我可能需要验证更多,但这就是我所能想到的,可以随意添加或修正)
我知道RegEx是最好的选择,我在Stack #1,#2上看到了其他的问题
使用另一个问题的RegEx,我得到了很好的结果,但它错过了一些我认为它应该抓住的
$arr = array (
'PO Box 123',
'P.O. Box 123',
'PO 123',
'Post Office Box 123',
'P.O 123',
'Box 123',
'#123', // no match
'123', // no match
'POB 123',
'P.O.B 123', // no match
'P.O.B. 123', // no match
'Post 123', // no match
'Post Box 123' // no match
);
foreach($arr as $po) {
if(preg_match("/^\s*((P(OST)?.?\s*O(FF(ICE)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i", $po)) {
echo "A match was found: $po\n";
} else {
echo "A match was not found: |$po| \n";
}
}为什么它不捕获数组中的最后两个值?
发布于 2011-03-01 19:53:49
到目前为止,在您的正则表达式中,“O”在“OFFICE”中是必需的。尝试^\s*((P(OST)?.?\s*(O(FF(ICE)?))?.?\s+(B(IN|OX))?)|B(IN|OX)) (在条件匹配中将'O‘分组)。
编辑:那应该是/^\s*((P(OST)?.?\s*(O(FF(ICE)?)?)?.?\s+(B(IN|OX))?)|B(IN|OX))/i。顺便说一句,http://rubular.com/是一个很好的正则表达式测试引擎。总是很高兴知道新的工具:)
发布于 2011-03-01 19:54:17
让我们经历一下..。
/ # Beginning of the regex
^ # Beginning of the string
\s* # (Any whitespace)
((
P # Matches your P
(OST)? # Matches your ost
.? # Matches the space
\s* # (Any whitespace)
O # Expects an O - you don't have one. Regex failed.发布于 2012-08-29 17:41:39
这个方法工作得更好,因为它移除了匹配集中不需要的组,只返回整个匹配。
跳槽员额123:
/^\s*((?:P(?:OST)?.?\s*(?:O(?:FF(?:ICE)?)?)?.?\s*(?:B(?:IN|OX)?)+)+|(?:B(?:IN|OX)+\s+)+)\s*\d+/i不是Skip Post 123:
/^\s*((?:P(?:OST)?.?\s*(?:O(?:FF(?:ICE)?)?)?.?\s*(?:B(?:IN|OX)?)?)+|(?:B(?:IN|OX)+\s+)+)\s*\d+/i删除末尾的\d+以跳过数字要求。
https://stackoverflow.com/questions/5159535
复制相似问题