概述
1:OCR读取出来的身份证地址字符串切割为省、市、区/县、详细地址
2:切割工具:
public class AddressSubUtils {
/**
* 切割地址字符串
* @param addr
* @return
*/
public static Map<String,Object> getAddress(String addr){
Map<String,Object> map = new HashMap();
String city =null;
String province = null;
String district=null;
String are = null;
String regex1="(?<province>[^自治区]+自治区|[^省]+省|[^市]+市)";
Matcher m1 = Pattern.compile(regex1).matcher(addr);
while (m1.find()){
province = m1.group("province");
if(province != null){
addr=addr.replaceFirst(province,"");
break;
}
}
String regex2="(?<city>[^辖区]+辖区|[^盟]+盟|[^自治州]+自治州|[^地区]+地区|[^市]+市|.+区划)";
Matcher m2 = Pattern.compile(regex2).matcher(addr);
while (m2.find()){
city = m2.group("city");
if (city != null){
addr=addr.replaceFirst(city,"");
break;
}
}
String regex3="(?<district>[^市]+市|[^县]+县|[^旗]+旗|.+区)";
Matcher m3 = Pattern.compile(regex3).matcher(addr);
while (m3.find()){
district = m3.group("district");
if (district != null){
addr=addr.replaceFirst(district,"");
break;
}
}
map.put("province",province);
map.put("city",city);
map.put("district",district);
map.put("address",addr);
return map;
}
}
3:由于有些身份证是没有省份,所以province可能会为空,需要加上判断
下面是调用的代码片段,这样就可以得出省市区,地址是已经导入到数据库中。
Map<String, Object> addr = AddressSubUtils.getAddress(address);//切割出来的地址集合
String province = (String)addr.get("province");
String city = (String)addr.get("city");
String district = (String)addr.get("district");
String addres = (String)addr.get("address");
String provinceCode ="";
String cityCode ="";
String districtCode ="";
if (province != null && !"".equals(province)){
provinceCode = areaMapper.selectByValue(province, "", "");
if (provinceCode==null || "".equals(provinceCode) ){
cityCode = areaMapper.selectByValue("", province, "");
if (cityCode != null && !"".equals(cityCode)){
provinceCode = areaMapper.selectParentCode(cityCode);
}
}
}
if (city != null && !"".equals(city)){
cityCode = areaMapper.selectByValue("", city, "");
if (province == null && "".equals(province)){
provinceCode = areaMapper.selectParentCode(cityCode);
}
if ("".equals(cityCode) || cityCode==null){
districtCode = areaMapper.selectByValue("", "", city);
if (districtCode != null && !"".equals(districtCode)){
cityCode = areaMapper.selectParentCode(districtCode);
if (provinceCode==null || "".equals(provinceCode)){
provinceCode = areaMapper.selectParentCode(cityCode);
}
}
}
}
if (district != null && !"".equals(district) && "".equals(districtCode)){
districtCode = areaMapper.selectByValue("", "", district);
if (cityCode == null && "".equals(cityCode)){
cityCode = areaMapper.selectParentCode(districtCode);
if (provinceCode == null && "".equals(provinceCode)){
provinceCode = areaMapper.selectParentCode(cityCode);
}
}
}
这是个人总结的一个方法,如果有更好方法的,欢迎留言,一起学习!
最后
以上就是清秀小鸽子为你收集整理的OCR读取身份证地址字符串切割成省、市、区的全部内容,希望文章能够帮你解决OCR读取身份证地址字符串切割成省、市、区所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复