Skip to content

Instantly share code, notes, and snippets.

@uri
Created June 27, 2016 21:11
Show Gist options
  • Save uri/45d41a49cf77f356a11eac36373ba0ab to your computer and use it in GitHub Desktop.
Save uri/45d41a49cf77f356a11eac36373ba0ab to your computer and use it in GitHub Desktop.
PO Box (Post office box) regular expression in ruby.
po_box_regex = /\A\b(?:p[.\s]?o?[.\s]?b?[.\s]?)\b(?:box)?|post{1}\s(?:office)?\s?(:?box|bin){1}\z/i
# Matches
#
# P. O. Box
# P.O.Box
# Post Box
# Post Office Box
# P.O.B
# P.O.B.
# POB
# Post Office Bin
# P O Box DesMoines1000
# P O Box Des Moines 1000
# Post Office Box
# Post Box
# PO
# P.O.
# P.B.
# Does NOT match
#
# Post Office
# Bin
# Box
# 12 Box
# Post
# Postal Code
# Portsmouth
# Campbell
# Raspberry
# Polonium
# Polar beer.
@alChaCC
Copy link

alChaCC commented Oct 5, 2018

it will fail when Po Shau Centre, Kwun Tong, Hong Kong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment