4.17. Find Addresses with Post Office Boxes

Problem

You want to catch addresses that contain a P.O. box, and warn users that their shipping information must contain a street address.

Solution

Regular expression

^(?:Post(?:Office)?|P[.]?O\.?)?Box\b
Regex options: Case insensitive, ^ and $ match at line breaks
Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

C#

Regex regexObj = new Regex(
    @"^(?:Post (?:Office )?|P[. ]?O\.? )?Box\b",
    RegexOptions.IgnoreCase | RegexOptions.Multiline
);
if (regexObj.IsMatch(subjectString) {
    Console.WriteLine("The value does not appear to be a street address");
} else {
    Console.WriteLine("Good to go");
}

Other programming languages

See Recipe 3.5 for help with implementing this regular expression with other programming languages.

Discussion

The following explanation is written in free-spacing mode, so each of the meaningful space characters in the regex has been escaped with a backslash:

^                # Assert position at the beginning of a line.
(?:              # Group but don't capture...
  Post\          #   Match "Post ".
  (?:Office\ )?  #   Optionally match "Office ".
 |               #  or...
  P[.\ ]?        #   Match "P" and an optional period or space character.
  O\.?\          #   Match "O", an optional period, and a space character.
)?               # Repeat the group between zero and one time.
Box              # Match "Box".
\b               # Assert position at a word boundary.
Regex options: Case insensitive, ^ and $ match at line breaks
Regex flavors: .NET, Java, PCRE, Perl, Python, Ruby

This regular expression matches all of the following example strings when they appear at the beginning of a line:

  • Post Office Box

  • post box

  • P.O. box

  • P O Box

  • Po. box

  • PO Box

  • Box

Despite the precautions taken here, you might encounter a few false positives or false negatives because many people are used to shippers being quite flexible in deciphering addresses. To mitigate this risk, it’s best to state up front that P.O. boxes are not allowed. If you get a match using this regular expression, consider warning the user that it appears she has entered a P.O. box, while still providing the option to keep the entry.

See Also

Recipes 4.14, 4.15, and 4.16

Get Regular Expressions Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.