Java实现根据地址智能识别省市区县

 更新时间:2025年03月05日 09:34:00   作者:Javaの甘乃迪  
这篇文章主要为大家详细介绍了如何编写一个Java工具类,可以根据身份证地址或用户输入的地址,智能识别并提取出详细的省市区县信息,感兴趣的小伙伴可以了解下

一、需求

根据OCR识别出的身份证地址或者用户输入的地址,智能识别出用户的省、市、区/县和详细地址信息。

二、思路

解析给到接口的完整地址fullAddress,逐个匹配省province、市city、区/县area、详细地址detail,然后将 province+city+area+detail 返回即可。

PS:需要考虑一些情况:

1、四个直辖市(北京市、上海市、天津市、重庆市)。直辖市的完整地址fullAddress只有省、县/区

2、完整地址fullAddress只有省、区/县

3、市与区/县重名

4、区/县与区/县重名

三、实现代码

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
 
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * 身份证信息分析工具类Util
 */
@Slf4j
public class AnalysisIdCardUtil {
 
    /**
     * 获取详细地址
     *
     * @param fullAddress 身份证完整地址
     */
    public static String getDetailedAddress(String fullAddress) {
        String[] addressArrays = spliceDetailedAddress(fullAddress);
        return addressArrays[addressArrays.length - 1];
    }
 
    /**
     * 获取省市区地址,如:安徽省合肥市包河区
     *
     * @param fullAddress 身份证完整地址
     */
    public static String getPcaAddress(String fullAddress) {
        String[] addressArrays = spliceDetailedAddress(fullAddress);
        StringBuilder areaBuffer = new StringBuilder();
        for (int i = 0; i < addressArrays.length - 1; i++) {
            if (StringUtils.isNotEmpty(addressArrays[i])) {
                areaBuffer.append(addressArrays[i]);
            }
        }
        return areaBuffer.toString();
    }
 
    /**
     * 身份证完成地址拆分为[省、市、区、详细地址]数组,如:[安徽省, 合肥市, 包河区, 幸福大街001号]
     *
     * @param fullAddress 身份证完整地址
     */
    public static String[] spliceDetailedAddress(String fullAddress) {
        String[] arr = new String[4];
        try {
            String tempStr = fullAddress;
            // 省
            String province = null;
            int provinceIdx = processProvince(tempStr);
            if (provinceIdx > -1) {
                province = tempStr.substring(0, provinceIdx + 1);
                tempStr = tempStr.substring(provinceIdx + 1);
            }
            // 市
            String city = null;
            int cityIdx = processCity(tempStr);
            if (cityIdx > -1) {
                city = tempStr.substring(0, cityIdx + 1);
                tempStr = tempStr.substring(cityIdx + 1);
            }
            // 区
            String county = null;
            int countyIdx = processCounty(tempStr);
            if (countyIdx > -1) {
                county = tempStr.substring(0, countyIdx + 1);
                tempStr = tempStr.substring(countyIdx + 1);
            }
            String street = tempStr;
            arr[0] = province;
            arr[1] = city;
            arr[2] = county;
            arr[3] = street;
        } catch (Exception e) {
            log.error("身份证详细地址转义失败:{}", e.getMessage());
        }
        return arr;
    }
 
    /**
     * 拆分身份证完整地址中的省份信息
     *
     * @param address 地址
     */
    private static int processProvince(String address) {
        int[] idx = new int[3];
        int provinceIdx;
        if ((provinceIdx = address.indexOf("省")) > -1) {
            idx[0] = provinceIdx;
        }
        if ((provinceIdx = address.indexOf("市")) > -1) {
            idx[1] = provinceIdx;
        }
        if ((provinceIdx = address.indexOf("区")) > -1) {
            idx[2] = provinceIdx;
        }
        Arrays.sort(idx);
        for (int j : idx) {
            if (j > 0) {
                return j;
            }
        }
        return provinceIdx;
    }
 
    /**
     * 拆分身份证完整地址中的市区/县/自治州信息
     *
     * @param address 地址
     */
    private static int processCity(String address) {
        int[] idx = new int[7];
        int cityIdx = -1;
        if ((cityIdx = address.indexOf("县")) > -1) {
            idx[0] = cityIdx;
        }
        if ((cityIdx = address.indexOf("自治州")) > -1) {
            idx[1] = cityIdx + 2;
        }
        if ((cityIdx = address.indexOf("市辖区")) > -1) {
            idx[2] = cityIdx + 2;
        }
        if ((cityIdx = address.indexOf("市")) > -1) {
            idx[3] = cityIdx;
        }
        if ((cityIdx = address.indexOf("区")) > -1) {
            idx[4] = cityIdx;
        }
        if ((cityIdx = address.indexOf("地区")) > -1) {
            idx[5] = cityIdx + 1;
        }
        if ((cityIdx = address.indexOf("盟")) > -1) {
            idx[6] = cityIdx;
        }
        Arrays.sort(idx);
        for (int j : idx) {
            if (j > 0) {
                return j;
            }
        }
        return cityIdx;
    }
 
    /**
     * 拆分身份证完整地址中的县/旗/岛信息
     *
     * @param address 地址
     */
    private static int processCounty(String address) {
        int[] idx = new int[6];
        int countyIdx;
        if ((countyIdx = address.indexOf("县")) > -1) {
            idx[0] = countyIdx;
        }
        if ((countyIdx = address.indexOf("旗")) > -1) {
            idx[1] = countyIdx;
        }
        if ((countyIdx = address.indexOf("海域")) > -1) {
            idx[2] = countyIdx + 1;
        }
        if ((countyIdx = address.indexOf("市")) > -1) {
            idx[3] = countyIdx;
        }
        if ((countyIdx = address.indexOf("区")) > -1) {
            idx[4] = countyIdx;
        }
        if ((countyIdx = address.indexOf("岛")) > -1) {
            idx[5] = countyIdx;
        }
        Arrays.sort(idx);
        for (int j : idx) {
            if (j > 0) {
                return j;
            }
        }
        return countyIdx;
    }
 
    /**
     * 身份证地址提取省市区
     *
     * @param fullAddress 身份证完整地址
     */
    public static Map<String, String> addressResolution(String fullAddress) {
        // 定义正则
        String regex = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<area>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<detail>.*)";
 
        Matcher matcher = Pattern.compile(regex).matcher(fullAddress);
        String province, city, area, town, detail;
        Map<String, String> map = new LinkedHashMap<>();
 
        while (matcher.find()) {
            province = matcher.group("province");
            map.put("province", StringUtils.isEmpty(province) ? "" : province.trim());
 
            city = matcher.group("city");
            map.put("city", StringUtils.isEmpty(city) ? "" : city.trim());
 
            area = matcher.group("area");
            map.put("area", StringUtils.isEmpty(area) ? "" : area.trim());
 
            town = matcher.group("town");
            map.put("town", StringUtils.isEmpty(town) ? "" : town.trim());
 
            detail = matcher.group("detail");
            map.put("detail", StringUtils.isEmpty(detail) ? "" : detail.trim());
        }
        return map;
    }
 
    public static void main(String[] args) {
 
        String address1 = "上海市上海市浦东新区世纪大道xx号上海中心大厦xx楼A座";
        String address2 = "内蒙古自治区呼伦贝尔市鄂温克族自治旗额尔古纳市阿尔山北路xxx号蒙古民族文化博物馆x楼xx展厅";
        String address3 = "广东省广州市番禺区沙湾镇大巷涌路xxx号";
 
        System.out.println("详细地址1:" + getDetailedAddress(address1));
        System.out.println("详细地址2:" + getDetailedAddress(address2));
        System.out.println("省市区地址拼接1:" + getPcaAddress(address1));
        System.out.println("省市区地址拼接2:" + getPcaAddress(address2));
        System.out.println("地址数组1:" + Arrays.toString(spliceDetailedAddress(address1)));
        System.out.println("地址数组2:" + Arrays.toString(spliceDetailedAddress(address2)));
 
        System.out.println("地址提取省市区:" + addressResolution(address2));
        System.out.println("地址提取省市区:" + addressResolution(address3));
    }
}

测试结果:

到此这篇关于Java实现根据地址智能识别省市区县的文章就介绍到这了,更多相关Java地址识别省市区县内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 浅谈Spring中的循环依赖问题与解决方案

    浅谈Spring中的循环依赖问题与解决方案

    这篇文章主要介绍了浅谈Spring中的循环依赖问题与解决方案,循环依赖就是两个或则两个以上的bean互相持有对方,最终形成闭环,比如A依赖于B,B依赖于C,C又依赖于A,需要的朋友可以参考下
    2023-12-12
  • IDEA项目代码上传gitlab远程仓库过程图解

    IDEA项目代码上传gitlab远程仓库过程图解

    这篇文章主要介绍了IDEA项目代码上传gitlab远程仓库过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • java计算指定日期为本年第几周的实例

    java计算指定日期为本年第几周的实例

    本文介绍了在编程中按周统计数据时遇到的问题,由于mysql中按所在月的周数周统计比较麻烦,采用所在年的周数作为分组条件,并通过java计算日期属于年的第一周来进行二次计算,提高性能,同时,作者也分享了计算指定日期为本年第几周的方法,并指出了一些常见方法的缺陷
    2025-11-11
  • StringUtils,CollectionUtils判断为空的方法和原生代码哪个效率最高

    StringUtils,CollectionUtils判断为空的方法和原生代码哪个效率最高

    这篇文章主要介绍了StringUtils,CollectionUtils判断为空的方法和原生代码哪个效率最高,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • 深入了解Java数据结构和算法之堆

    深入了解Java数据结构和算法之堆

    这篇文章主要为大家介绍了Java数据结构和算法之堆 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • 聊聊Java 中的线程中断

    聊聊Java 中的线程中断

    这篇文章主要介绍了Java 中的线程中断的相关资料,帮助大家更好的理解和使用Java,感兴趣的朋友可以了解下
    2020-11-11
  • 部署在linux上的java服务老是挂掉问题排查日志

    部署在linux上的java服务老是挂掉问题排查日志

    当服务器挂掉时,操作系统通常会记录一些异常信息,检查服务器的系统日志可以帮助定位问题所在,这篇文章主要介绍了部署在linux上的java服务老是挂掉问题排查日志的相关资料,需要的朋友可以参考下
    2025-10-10
  • SpringCloud微服务踩坑记录分享

    SpringCloud微服务踩坑记录分享

    本文记录了作者在使用SpringCloud微服务时遇到的问题,首先,作者尝试修改配置文件中的service-name和instance-id,但仍然无法解决问题,后来,作者尝试更换SpringCloud版本为2.2.5,并搭配Hoxton.SR3版本,问题得以解决
    2024-11-11
  • Java计算经纬度距离的示例代码

    Java计算经纬度距离的示例代码

    在 Java 中计算两个经纬度之间的距离,可以使用多种方法(代码示例均返回米为单位),文中整理了常用的5种方法,感兴趣的小伙伴可以了解一下
    2025-05-05
  • Java中ArrayList与顺序表的定义与实现方法

    Java中ArrayList与顺序表的定义与实现方法

    ArrayList是一个实现List接口的类,底层是动态类型顺序表,本质也就是数组,动态主要体现在它的扩容机制,下面这篇文章主要给大家介绍了关于Java中ArrayList与顺序表的定义与实现的相关资料,需要的朋友可以参考下
    2022-07-07

最新评论