Ge tCity State Zip REGEX

by ssi 17. December 2015 17:43

   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;

        }

 

Tags:

CSharp | Regex

Add comment

biuquote
  • Comment
  • Preview
Loading

Calendar

<<  February 2026  >>
MoTuWeThFrSaSu
2627282930311
2345678
9101112131415
16171819202122
2324252627281
2345678

View posts in large calendar

RecentComments

None

Development Team @ Shelbysys

We develop custom database applications for our clients. Our development tool of choice is MS Visual Studio. 

Quotations

"Fill the unforgiving minute with sixty seconds worth of distance run."
Rudyard Kipling