[.Net] 用正則拆解中文地址

  • 5932
  • 0
  • 2018-09-07

摘要:[.Net] 用正則拆解中文地址

前言

因工作需要將客戶傳進來的地址作拆解分類看了前人寫的code是使用indexOf + Substring 來拆解一整串的地址。

針對這種土法煉鋼的方式覺得雖然是個解法,但不是好的解法。而且針對台灣的地址,也不一定能完全滿足substring時index的規則。

所以想用正則來使code乾淨簡潔,還在學習使用中,有錯請多包容。

需求:

給部分或完整地址皆能拆解分類至細項

實作:

1.建個ADDRESS類別,建構式傳入原始地址

2.相關屬性 :City 為縣市,Region為鄉鎮市區,Village為村里,Neighbor為鄰,Road為路,Section為段,Lane為巷,Alley為弄,No為號,Seq為序號,Floor為樓層,other為其他。


public class ADDRESS
    {
        public ADDRESS(string address)
        {
            this.OrginalAddress = address;
            this.ParseByRegex(address);
        }

        public static string GetDTName = "SELECT";

        /// 

        /// 縣市
        /// 

        public string City { get; set; }

        /// 

        /// 鄉鎮市區
        /// 

        public string Region { get; set; }

        /// 

        /// 村里
        /// 

        public string Village { get; set; }

        /// 

        /// 鄰
        /// 

        public string Neighbor { get; set; }

        /// 

        /// 路
        /// 

        public string Road { get; set; }
        
        /// 

        /// 段
        /// 

        public string Section { get; set; }

        /// 

        /// 巷
        /// 

        public string Lane { get; set; }
        
        /// 

        /// 弄
        /// 

        public string Alley { get; set; }
        
        /// 

        /// 號
        /// 

        public string No { get; set; }
        
        /// 

        /// 序號
        /// 

        public string Seq { get; set; }
        
        /// 

        /// 樓
        /// 

        public string Floor { get; set; }

        public string Others { get; set; }

        /// 

        /// 是否符合pattern規範
        /// 

        public bool IsParseSuccessed { get; set; }

        /// 

        /// 原始傳入的地址
        /// 

        public string OrginalAddress { get; private set; }

        private void ParseByRegex(string address)
        {
            var pattern = @"(?\D+?[縣市])(?\D+?(市區|鎮區|鎮市|[鄉鎮市區]))?(?\D+?[村里])?(?\d+[鄰])?(?\D+?(村路|[路街道段]))?(?
\D?段)?(?\d+巷)?(?\d+弄)?(?\d+號?)?(?-\d+?(號))?(?\d+樓)?(?.+)?";
            
            Match match = Regex.Match(address, pattern);
            
            if (match.Success)
            {
                this.IsParseSuccessed = true;
                this.City = match.Groups["city"].ToString();
                this.Region = match.Groups["region"].ToString();
                this.Village = match.Groups["village"].ToString();
                this.Neighbor = match.Groups["neighbor"].ToString();
                this.Road = match.Groups["road"].ToString();
                this.Section = match.Groups["section"].ToString();
                this.Lane = match.Groups["lane"].ToString();
                this.Alley = match.Groups["alley"].ToString();
                this.No = match.Groups["no"].ToString();
                this.Seq = match.Groups["seq"].ToString();                
                this.Floor = match.Groups["floor"].ToString();
                this.Others = match.Groups["others"].ToString();
            }
            
        }
}

總結:

因拆解分類較細且一開始沒考慮到路名在有段沒有路(澎湖縣馬公市鎖港里鎖管港段)而產生錯誤,花了一段時間去研究如何拆解。

因此除了一些較特殊的地址如釣魚台 南海島 南沙群島等,其它大部分都可解析出來。

附上線上正則網頁和地址如下:

http://www.rubular.com/

臺北市信義區市府路1號
北市大安區忠孝東路四段101巷45號
北市信義區吳興街220巷11弄39號
北市三重路66-2號14樓
北市南港區合成里1鄰中坡北路30巷19-2號3樓之3
北市八德路四段145巷6弄12-2號5樓
苗栗縣苗栗市嘉盛里25鄰為公路584號
南投縣仁愛鄉大同村定遠新村路18-1號
連江縣南竿鄉介壽村3鄰48-2號
澎湖縣馬公市鎖港里鎖管港段1439號
台中市順天路250號

以上地址如有雷同存屬巧合

本文章參考網址:http://www.dotblogs.com.tw/hatelove/archive/2012/06/05/parse-taiwan-address-with-regex.aspx