• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 708
  • Last Modified:

Best regex for POBox address capture

/^\s*((p(ost)?.?\s*(o(ff(ice)?)?)?.?\s+(b(in|ox))?)|b(in|ox))/i

Open in new window

Is the above the best regex to capture Post Office Box addresses?

Using the above, it failed on the ones marked below:
P.O. Box 123
PO 123
Post Office Box 123
P.O 123
Box 123
#123       // This one
123        // This one
POB 123
P.O.B 123  // This one
P.O.B. 123 // This one
Post 123
Post Box 123

Open in new window

Any more you can think of or how would I modify it to also capture those missed?
0
Eddie Shipman
Asked:
Eddie Shipman
  • 3
  • 3
  • 3
2 Solutions
 
Terry WoodsIT GuruCommented:
The best regex is the one that works for you - each person may have different criteria, so there's no absolute best answer for everyone.

The following pattern matches all the cases you list, but if you are capturing data out of a larger piece of text then you might find that it also captures data that you don't want to capture. There's probably no avoiding that problem if you want to pick up a value like "123". If this expression doesn't work for you, please provide some further examples of what to match and to not match.
/^\s*((p(ost)?.?\s*(o(ff(ice)?)?)?\W?\s*(b(in|ox)?)?)|b(in|ox))?\W*\d+/i

Open in new window

0
 
Terry WoodsIT GuruCommented:
Further to my comment "There's probably no avoiding that problem if you want to pick up a value like 123", I say this because you may need an address parser to determine what's a street or apartment number rather than a PO Box number.
0
 
Ray PaseurCommented:
The USPS offers an address standardization service.  You might want to run the addresses through their service before you try to parse them.  I think you will find many fewer variants after the addresses are standardized.
https://www.usps.com/webtools/htm/Address-Information-v3-1a.htm
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Eddie ShipmanAll-around developerAuthor Commented:
Essentially, Ray, this is to prohibit UPS shipping for POBox addresses in Magento. I don't have time to work with the USPS service to get the addresses standardized.
0
 
Ray PaseurCommented:
I don't have time to work with the USPS service ?? It's instantaneous!
0
 
Eddie ShipmanAll-around developerAuthor Commented:
No, I don't have time to figure out how to use it. Besides, we don't want to modify the addresses, just determine if it is a PO Box.
0
 
Ray PaseurCommented:
If you are going to ship any products, it might be wise to normalize the addresses.  I say this because my web sites have shipped thousands of products and we always normalized the addresses -- it's just a "best practice."  On the other hand, why not let your customer choose the method of shipping.?  The UPS API will not accept PO boxes.
0
 
Terry WoodsIT GuruCommented:
Can I help any further? I haven't had any feedback on my suggested solution.
0
 
Eddie ShipmanAll-around developerAuthor Commented:
Because the store is not a full Magento store, we essentially only use the cart, we want to control the shipping options. The way it is built, it would not prohibit a POBox address from selecting UPS shipping and, as the publications we ship are shipped by a 3rd-party, they ship based on an export that we create that tells them the shipping method, product, customer info, etc.

Thanks. I've got it all worked out now.

@JerryAtOpus, No comments on the solution because we decided that capturing those others was not a problem.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now