U.S. ZIP Codes
ZIP codes were introduced to the U.S. in 1963 (ZIP is actually an acronym for Zone Improvement Plan). There are more than 40,000 U.S. ZIP codes, all made up of digits (the first digit is assigned from East to West, with 0 being on the East Coast and 9 being on the West Coast). In 1983, the post office began using an expanded ZIP code called ZIP+4. The extra four digits provide a greater level of granularity (often a specific city block or sometimes a specific building), which in turn provides for greater mail reliability. Use of ZIP+4 is optional, and as such, ZIP-code validation usually must be able to accommodate both five-digit ZIP codes and ZIP+4 (with a hyphen separating the first five digits from the additional four digits).
999 1st Avenue, Bigtown, NY, 11222 123 High Street, Any City, MI 48034-1234
\ d{ 5} (-\ d{ 4} )?
999 1st Avenue, Bigtown, NY, 11222 123 High Street, Any City, MI 48034-1234
\ d{ 5} matches any five digits, and -\ d{ 4} matches a hyphen followed by the +4 digits. Because these additional four digits are essentially optional, -\ d{ 4} is enclosed within parentheses (turning it into a subexpression), and ? is used to optionally allow a single instance of that subexpression.