C# 正则提取省市区地址
/// <summary> /// 解析省市区 /// </summary> /// <param name="address"></param> /// <returns></returns> public (string province,string city,string county,string town,string village) Analysis(string address) { string regex = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)?(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)?(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)"; var m = Regex.Match(address, regex, RegexOptions.IgnoreCase); var province = m.Groups["province"].Value; var city = m.Groups["city"].Value; var county = m.Groups["county"].Value; var town = m.Groups["town"].Value; var village = m.Groups["village"].Value; return (province, city, county, town, village); } /// <summary> /// 调用 /// </summary> public void Call() { var res = Analysis("江西省南昌市青云谱区广州路25号"); var province = res.province;//省 var city = res.city;//市 var county = res.county;//县 var town = res.town;//镇 var village = res.village;//村 }
转:https://blog.csdn.net/lscV001/article/details/118934339?spm=1035.2023.3001.6557&utm_medium=distribute.pc_relevant_bbs_down.none-task-blog-2~default~OPENSEARCH~default-5.nonecase&depth_1-utm_source=distribute.pc_relevant_bbs_down.none-task-blog-2~default~OPENSEARCH~default-5.nonecase