by ssi
  1. May 2013 17:40
  
  Useage:   string[] CityStateZip = GetCityStateZip(cus.ADD3);
 
private string[] GetCityStateZip(string sAddress)
        {
            //string[] split = sAddress.Split(new Char[] { ' ', });
            //return split;
            sAddress = sAddress.Replace(".", "");
            sAddress = sAddress.Replace(",", ", ");
            sAddress = sAddress.Replace("  ", " ");
            string[] aRet = new string[3];
            Regex addressPattern = new Regex(@"(?<city>[A-Za-z',.\s]+) (?<state>([A-Za-z]{2}|[A-Za-z]{2},))\s*(?<zip>\d{5}(-\d{4})|\d{5})");
 
            MatchCollection matches = addressPattern.Matches(sAddress);
 
            for (int mc = 0; mc < matches.Count; mc++)
            {
                aRet[pADDR_CITY] = matches[mc].Groups["city"].Value;
                aRet[pADDR_STATE] = matches[mc].Groups["state"].Value;
                 aRet[pADDR_ZIP] = matches[mc].Groups["zip"].Value;
            }
            return aRet;
        }
 
  
    a6fe347d-d67d-42ce-993e-187cc54406c6|0|.0|96d5b379-7e1d-4dac-a6ba-1e50db561b04
    Tags: regex, array, match
    Regex