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?
LVL 26
Eddie ShipmanAll-around developerAsked:
Who is Participating?
 
Terry WoodsConnect With a Mentor IT 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 PaseurConnect With a Mentor Commented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.