0

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 
Matt
  • 22,721
  • 17
  • 71
  • 112
Arasoi
  • 25
  • 1
  • 8
  • 2
    More to the point, how are you differentiating between the street and the city? What about roads with multiple words before the St? e.g. Glen Eyre Road – Paul Alan Taylor Jan 11 '10 at 19:20
  • what I was doing which is a rather poor way of doing it. Was to First split the string on the comma giving me the 2 strings one with the house number street and city , the other with the state and zipcode. from there i split on spaces. For the State zip this was easy to deal with only to items returned. As to the addie its self i would accout for long street name by taking the bottom item and top item off the array to get house number and city then combine the remaing into street. I big mess and not very acurate thus my quesiton here. – Arasoi Jan 11 '10 at 19:40
  • Where is the rest of your code? That is incomplete code and not useful. – TetraDev Apr 04 '16 at 20:14

4 Answers4

2

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
  • 74,861
  • 18
  • 132
  • 169
  • 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 Jan 12 '10 at 15:04
0
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
  • 510,854
  • 105
  • 1,084
  • 1,005
0

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
  • 6,444
  • 6
  • 38
  • 52
0

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.