views:

146

answers:

5

Ok folks I have bombed around for a few days trying to find a good solution for this one. What I have is two possible address formats.

28 Main St Somecity, NY 12345-6789    
or 
Main St Somecity, Ny 12345-6789

What I need to do Is split both strings down into an array structured as such

address[0] = HousNumber 
address[1] = Street 
address[2] = City 
address[3] = State 
address[4] = ZipCode

My major problem is how to account for the lack of a house number. with out having the whole array shift the data up one.

address[0] = Street 
address[1] = City 
address[2] = State 
address[3] = ZipCode

[Edit]

For those that are wondering this is what i am doing atm . (cleaner version)

     place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    FCmap.setCenter(point,12);


        var a = place.address.split(',');

        var e = a[2].split(" ");

        var x = a[0].split(" ");

        var hn = x.filter(function(item,index){
            return index == 0;
        });

                var st = x.filter(function(item,index){
                    return index != 0;
                });

                var street = ''; 

                st.each(function(item,index){street += item + ' ';});

        results[0] = new Hash({
                        FullAddie:  place.address,
                        HouseNum:   hn[0],
                        Dir:        '',
                        Street:     street,
                        City:       a[1],
                        State:      e[1],
                        ZipCode:    e[2],
                        GPoint:     new GMarker(point),
                        Lat:        place.Point.coordinates[1],
                        Lng:        place.Point.coordinates[0]
                        });
        // End Address Splitting 
+2  A: 

Reverse the string, do the split and then reverse each item.

Update: From the snippet you posted, it seems to me that you get the address from a Google GClientGeocoder Placemark. If that is correct, why are you getting the unstructured address (Placemark.address) instead of the structured one (Placemark.AddressDetails)? This would make your life easier, as you would have to try and parse only the ThoroughfareName, which is the street level part of the address, instead of having to parse everything else as well.

Franci Penov
Thank you I was actually unaware of the Placemark.Address.Details . With that it helps solve most of my problem leaving me with only trying to find a way to handle the lack of a house number. Which i can better deal with based on the accuracy level returned by Google now.
Arasoi
A: 
function get_address (addr_str) {
   var m = /^(\d*)\s*([-\s\w.]+\s(?:St|Rd|Ave)\.?)\s+([-\s\w\.]+),\s*(\w+)\s+([-\d]+)$/i.exec(s);
   var retval = m.slice(1);
   if (!retval[0]) retval = retval.slice(1);
   return retval;
}

Assume all streets ends with St, Rd or Ave.

KennyTM
except in the case where they end up with Ct, Pl, Way or have no suffix like this. :-)
Franci Penov
Or if it's a "Some St (as in Street) St (as in Saint) Someone City" :)
KennyTM
A: 

If your data is always going to be in one of the two formats then you should use a regular expression to break up the address.

/([\d\w ]+), (\w+) ([\d-]+)/ 

should do it.

var address1 = "Main St Somecity, Ny 12345-6789";
var address2 = "28 Main St Somecity, NY 12345-6789"
var pattern = /([\d\w ]+), (\w+) ([\d-]+)/;
var res = pattern.exec(address1);
alert("address: " + res[1] + " | state: " + res[2] + " | zip: " + res[3]);
res = pattern.exec(address2);
alert("address: " + res[1] + " | state: " + res[2] + " | zip: " + res[3]);
sberry2A
A: 

var address = /[0-9]/.match(string.charAt(0)) ? string.split(" ") : [ " " ].concat(string.split(" "));

This is not particularly robust, but it accounts for the two enumerated cases and is concise at only one line.

Willi Ballenthin
A: 

I've got a similar problem I'm trying to solve. It seems that if you look for the first space to the right of the house number, you can separate the house number from the street name.

Here in Boston you can have a house number that includes a letter! In addition, I've seen house numbers that include "1/2". Luckily, the 1/2 is preceded by a hyphen, so there aren't any embedded spaces in the house number. I don't know if that's a standard or if I'm just getting lucky.

Mark Giese