private SmartAnalysisResult SmartAnalysisAddress(List<tm_master_city_PO> regions, string memo) { var result = new SmartAnalysisResult(); if (string.IsNullOrEmpty(memo)) return null; var areas = regions.Where(t => t.RegionType == "4").ToList(); var citys = regions.Where(t => t.RegionType == "3").ToList(); var provinces = regions.Where(t => t.RegionType == "2").ToList(); tm_master_city_PO area=null; var strArray = memo.Split(new char[] { ',',',',':',':',' ','!','!','.','。','|'}).Select(x=>x.Trim()).OrderBy(x=>x.Length); //如果先按 区镇来查,会出现 不同市下相同名称的区镇,没法确定唯一性 var province = provinces.FirstOrDefault(x => memo.IndexOf(x.RegionName) > -1); if(province==null) { province = provinces.FirstOrDefault(x => memo.IndexOf(x.RegionName.Replace("省", "")) > -1); } if(province!=null) { result.ProvinceID = province.CityID; result.ProvinceName = province.RegionName; var city = citys.FirstOrDefault(x => x.ParentCityID == result.ProvinceID && memo.IndexOf(x.RegionName) > -1); area = areas.FirstOrDefault(x => memo.IndexOf(x.RegionName) > -1); if (city==null&& area==null) { city = citys.FirstOrDefault(x => x.ParentCityID == result.ProvinceID && memo.IndexOf(x.RegionName.Replace("市", "")) > -1); } if (city != null) { result.CityID = city.CityID; result.CityName = city.RegionName; area = areas.FirstOrDefault(x => x.ParentCityID == result.CityID && memo.IndexOf(x.RegionName) > -1); if(area==null) { area = areas.FirstOrDefault(x => x.ParentCityID == result.CityID && memo.IndexOf(x.RegionName.Replace("区", "").Replace("镇", "")) > -1); } if (area != null) { result.AreaID = area.CityID; result.AreaName = area.RegionName; } } else { area = areas.FirstOrDefault(x =>memo.IndexOf(x.RegionName) > -1); if (area == null) { area = areas.FirstOrDefault(x => memo.IndexOf(x.RegionName.Replace("区", "").Replace("镇", "")) > -1); } if (area != null) { result.AreaID = area.CityID; result.AreaName = area.RegionName; result.CityID = area.ParentCityID; result.CityName = citys.FirstOrDefault(x => x.CityID == result.CityID).RegionName; } } } foreach (var str in strArray) { if (string.IsNullOrWhiteSpace(str)) { continue; } var areaIndex = str.IndexOf(result.AreaName); if (areaIndex > 0) { result.Address = str.Substring(areaIndex + result.AreaName.Length); } var phones = GetTelephoneList(str); if (phones != null && phones.Count > 0) { result.TelPhone = phones.First(); } } foreach (var str in strArray) { var str1 = str.Replace("手机", "").Replace("电话", ""); if (!string.IsNullOrWhiteSpace(result.ProvinceName)) { str1 = str1.Replace(result.ProvinceName, ""); } if (!string.IsNullOrWhiteSpace(result.CityName)) { str1 = str1.Replace(result.CityName, ""); } if (!string.IsNullOrWhiteSpace(result.AreaName)) { str1 = str1.Replace(result.AreaName, ""); } if (string.IsNullOrWhiteSpace(str1)) { continue; } string nameStr = ""; var telPhoneIndex = str1.IndexOf(result.TelPhone); if(telPhoneIndex>0) { nameStr = SmartAnalysisName(str1.Substring(telPhoneIndex + result.TelPhone.Length)); if (!string.IsNullOrWhiteSpace(nameStr)) { result.ContactUser = nameStr; break; } } if (!string.IsNullOrWhiteSpace(result.TelPhone)) { str1 = str1.Replace(result.TelPhone, ""); } nameStr = SmartAnalysisName(str1); if (!string.IsNullOrWhiteSpace(nameStr)) { result.ContactUser = nameStr; break; } } if (!string.IsNullOrWhiteSpace(result.TelPhone)) { result.Address = result.Address.Replace(result.TelPhone, ""); } if (!string.IsNullOrWhiteSpace(result.ContactUser)) { result.Address = result.Address.Replace(result.ContactUser, ""); } return result; }
private string SmartAnalysisName(string name) { if(name==null) { return null; } name = name.TrimEnd('收'); var index = name.IndexOf("特殊名称"); name = name.Replace("特殊名称", ""); if (string.IsNullOrWhiteSpace(name)) { return null; } if (name.Length>1&&name.Length<6) //认为是姓名 { if(index==-1) { return name; } else if(index==1) { return "特殊名称" + name; } else { return name + "特殊名称"; } } return null; }
/// <summary> /// 提取字符串中的电话号 /// * 兼容11位手机号、3位或4位区号+8位电话号、400电话的两种写法(400-ddd-dddd、400ddddddd) /// </summary> /// <param name="input">输入的字符串</param> /// <returns></returns> private List<string> GetTelephoneList(string input) { //集合存放提取出来的电话号码 List<string> list = new List<string>(); /* * 正则表达式提取 * 分为五种格式,能兼容11位手机号、3位或4位区号-7位或8位电话号、400电话的两种写法(400-ddd-dddd、400ddddddd) */ Regex regex = new Regex(@"(1[3|4|5|6|7|8|9]\d{9})|(0\d{2,3}-\d{7,8})|(400-\d{3}-\d{4})|(400\d{7})"); //Regex regex = new Regex(@"(1[3|4|5|6|7|8|9]\d{9})"); //Match集合,匹配成功的字符串集合 MatchCollection collection = regex.Matches(input); //遍历Match集合,取出值 string telephone; foreach (Match item in collection) { foreach (Group group in item.Groups) { telephone = group.Value.Trim(); //偶尔会出现重复提取,所以加了去重判断 if (!string.IsNullOrEmpty(telephone) && !list.Contains(telephone)) { list.Add(telephone); } } } return list; }
/// <summary> /// 省市区表(tm_master_city) /// </summary> public partial class tm_master_city_PO { /// <summary> /// CityID /// </summary> public int CityID { get; set; } /// <summary> /// ParentCityID /// </summary> public int ParentCityID { get; set; } /// <summary> /// RegionName /// </summary> public string RegionName { get; set; } /// <summary> /// RegionType /// </summary> public string RegionType { get; set; } /// <summary> /// PostNo /// </summary> public string PostNo { get; set; } }
输入:memo:浙江省海宁市海昌南路498号金汇大厦,黄赵画15067366587
输出:
{ "contactUser":"黄赵画", "telPhone":"15067366159", "provinceID":330000, "provinceName":"浙江省", "cityID":330400, "cityName":"嘉兴市", "areaID":330481, "areaName":"海宁市", "address":"海昌南路498号金汇大厦" }
标签:null,string,area,信息,Replace,result,收件,解析,RegionName From: https://www.cnblogs.com/nanxixi/p/17970070