Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试使用正则表达式来检查完整的地址字符串是否包含邮政编码,有人可以帮我吗?
$address = '10100 Trinity Parkway, 5th Floor Stockton, CA 95219'; if (preg_match('--regex_here--',$ad)) { echo 'true'; }
通常邮政编码遵循两个字母的州代码,因此类似以下的内容应该是相当可靠的:
/\b[A-Z]{2}\s+\d{5}(-\d{4})?\b/
解释:
\b # word boundary [A-Z]{2} # two letter state code \s+ # whitespace \d{5} # five digit zip (-\d{4})? # optional zip extension \b # word boundary
您也可以\b将末尾更改为,$因为邮政编码通常(总是?)在地址的末尾。
\b
$