-2

I have an array of address but some of them are not valid they either start with an apartment like

XYZ rd   {Alphabatical start}

or it has a range like

4000-9000 wallace rd

etc

Obviously I cant ship my products to those addresses. Can someone give me an regex

EDIT

  1. I need only the addresses that start with some integers
  2. I do not want range addresses like 4009-4015 terrace ave, etc

thanks

Matt
  • 22,721
  • 17
  • 71
  • 112
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221
  • 4
    No one can supply a regular expression until you detail what regular pattern your addresses should follow. (and it won't be easy) – Michael Berkowski Mar 22 '12 at 20:39
  • 5
    There's no good catch-all way to validate a street address using regex. – WWW Mar 22 '12 at 20:40
  • 3
    A regex for this would be impossible. Plus why is a range invalid? Ever been to NYC? There are addresses like `105-12 Queens Blvd`, this is not a range, it's one building (one address). I suggest looking into an API (like the [USPS WebTools API](https://www.usps.com/business/webtools.htm)) to validate addresses. Also, I'm not sure if all apartment addresses are necessarily P.O. boxes (where you can't deliver packages to). – gen_Eric Mar 22 '12 at 20:40
  • 1
    At my own company, we've identified well over 3 dozen different, incompatible, common address formats, few of which are reasonably parsed with a regex. There are so many different exceptions, that we essentially gave up on finding a basic format. Instead, we *impose* a format on our customers. – greyfade Mar 22 '12 at 20:49
  • Why don't you want "range addresses" (the `-` doesn't necessarily mean it's a "range", it could be one address) or addresses starting with letters? Also, apartment addresses are usually `15 Green Drive Apt. D` or `15D Green Drive`, they (usually) don't start with `Apt.`. Again, I suggest validating address with an API (like the USPS or UPS), not regexes. – gen_Eric Mar 22 '12 at 20:50
  • "Obviously I cant ship my products to those addresses" Why not? – gen_Eric Mar 22 '12 at 20:54
  • 2
    wait till you want to validate address all over the world! –  Mar 22 '12 at 20:54

2 Answers2

5

I suggest using the UPS or USPS (or FedEx, etc.) address validation APIs, instead of a regex. This way you'll know the address is really real, and not just that it looks real.

I mean, if you're gonna ship something, you wouldn't actually ship it to 123 Fake Street. That would pass a regex, but it's not real.

Links:

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

Your best bet is to enusure that the test is alpha numeric (with a couple of other symbols), with at least two lines. Also check that the post code (UK), Zip code (US) etc is valid. The latter two I you can check with a regex.

Otherwise just rely on the common sense of the post office (shipping service provider for the Americans on this MB).

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • Although the *real* goal of the OP (ensuring valid shipping address) cannot be solved by regex alone, your suggestion is still not useful. Alpha-numeric? Too strict. "rely on the common sense of the postie"? Not sure what that means exactly, but if you mean "don't do any validation" then I would suggest that is also a bad idea. By the way, I'm pretty sure we're just talking about the street address, not the whole thing. – Wesley Murch Mar 22 '12 at 20:55
  • Saw your edit: "with a couple of other symbols". Which ones would those be? For example: is `/` or `*` guaranteed to *never* occur in a valid address? What about `asdfasdfasdf`, just accept it? – Wesley Murch Mar 22 '12 at 20:58
  • It's also a good idea to catch *mistakes* rather than intentional garbage addresses (which I agree, are not likely). – Wesley Murch Mar 22 '12 at 21:04
  • 1
    Just to ensure that the address is printable and I would choose alpha numeric, commas, / to ensure that the address is reasonable. If the address is not perfect, the postie usual can figure it out. As for your other example I have not found out a suitable way of ensuring that people put rubbish into the system - but I assume that if they are paying for something they will not try to do that. The British system is quite down with just the post code and house number. It has been known that all is required is the name of the person and the post code. – Ed Heal Mar 22 '12 at 21:06
  • At the end of the day just need to try to filter out the obvious mistakes and rubbish addresses. Could even use the post code to check that the address matches. – Ed Heal Mar 22 '12 at 21:07
  • I misinterpreted "postie", thought you meant the user supplying the address. Must be a UK thing. I agree, whoever handles the delivery will be able to figure it out if the address exists and where to bring the package. They will hold it at the post office otherwise. – Wesley Murch Mar 22 '12 at 21:08
  • @EdHeal: Is that a UK term? Never heard it used here in the US. – gen_Eric Mar 22 '12 at 21:12
  • 1
    It is a term of endearment for postmen. Us English people struggle to teach you yanks English! – Ed Heal Mar 22 '12 at 21:15