Java 切割身份证地址,省市区 详细地址工具类

工具类①:


AddressAnalysisUtil.java

import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;

public class AddressAnalysisUtil {

    public static void main(String[] args) {
        String address= AddressAnalysisUtil.covertOnlyDetailAddress("重庆市渝北区龙山大道xx号x幢2xxx");
        System.out.println(address);
        String pcdStr= AddressAnalysisUtil.covertFullPCD("内蒙古自治区兴安盟科尔沁右翼前旗");
        System.out.println(pcdStr);
        String[] addressArray = AddressAnalysisUtil.convertDetailAddressToPCD("山东省临沂市兰山区李官镇小李官村XXXXX号");
        System.out.println(Arrays.asList(addressArray).toString());
    }

    /**
     * 只获取出详细地址
     *
     * @param fullAddress
     * @return
     */
    public static String covertOnlyDetailAddress(String fullAddress) {
        String[] addressArrays = convertDetailAddressToPCD(fullAddress);
        return addressArrays[addressArrays.length - 1];
    }

    /**
     * 只获取出省市区的拼接
     *
     * @param fullAddress
     * @return
     */
    public static String covertFullPCD(String fullAddress) {
        String[] addressArrays = convertDetailAddressToPCD(fullAddress);
        StringBuffer pdcStrBuffer=new StringBuffer();
        for (int i = 0; i < addressArrays.length-1; i++) {
            if (StringUtils.isNotEmpty(addressArrays[i])){
                pdcStrBuffer.append(addressArrays[i]);
            }
        }
        return pdcStrBuffer.toString();
    }

    /**
     * 处理详细地址拆分省 市 区 地址的转换关系
     *
     * @param detailAddress
     * @return
     */
    public static String[] convertDetailAddressToPCD(String detailAddress) {
        String[] r = new String[4];
        try {
            String tempStr = detailAddress;
            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;
            r[0] = province;
            r[1] = city;
            r[2] = county;
            r[3] = street;
        } catch (Exception e) {
            // 报错就直接返回r 为空即可。无法正常转义
        }

        return r;
    }

    // (?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)
    private static int processProvince(String s) {
        int[] idxs = new int[3];
        int provinceIdx = -1;
        if ((provinceIdx = s.indexOf("省")) > -1)
            idxs[0] = provinceIdx;
        provinceIdx = -1;
        if ((provinceIdx = s.indexOf("市")) > -1)
            idxs[1] = provinceIdx;
        provinceIdx = -1;
        if ((provinceIdx = s.indexOf("区")) > -1)
            idxs[2] = provinceIdx;
        Arrays.sort(idxs);
        for (int i = 0; i < idxs.length; i++) {
            int j = idxs[i];
            if (j > 0) {
                return j;
            }
        }

        return provinceIdx;
    }

    // (?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)
    private static int processCity(String s) {
        int[] idxs = new int[7];
        int cityIdx = -1;
        if ((cityIdx = s.indexOf("县")) > -1)
            idxs[0] = cityIdx;
        cityIdx = -1;
        if ((cityIdx = s.indexOf("自治州")) > -1)
            idxs[1] = cityIdx + 2;
        cityIdx = -1;
        if ((cityIdx = s.indexOf("市辖区")) > -1)
            idxs[2] = cityIdx + 2;
        cityIdx = -1;
        if ((cityIdx = s.indexOf("市")) > -1)
            idxs[3] = cityIdx;
        cityIdx = -1;
        if ((cityIdx = s.indexOf("区")) > -1)
            idxs[4] = cityIdx;
        cityIdx = -1;
        if ((cityIdx = s.indexOf("地区")) > -1)
            idxs[5] = cityIdx + 1;
        cityIdx = -1;
        if ((cityIdx = s.indexOf("盟")) > -1)
            idxs[6] = cityIdx;

        Arrays.sort(idxs);

        for (int i = 0; i < idxs.length; i++) {
            int j = idxs[i];
            if (j > 0) {
                return j;
            }
        }

        return cityIdx;
    }

    // (?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)
    private static int processCounty(String s) {
        int[] idxs = new int[6];
        int countyIdx = -1;
        if ((countyIdx = s.indexOf("县")) > -1)
            idxs[0] = countyIdx;
        countyIdx = -1;
        if ((countyIdx = s.indexOf("旗")) > -1)
            idxs[1] = countyIdx;
        countyIdx = -1;
        if ((countyIdx = s.indexOf("海域")) > -1)
            idxs[2] = countyIdx + 1;
        countyIdx = -1;
        if ((countyIdx = s.indexOf("市")) > -1)
            idxs[3] = countyIdx;
        countyIdx = -1;
        if ((countyIdx = s.indexOf("区")) > -1)
            idxs[4] = countyIdx;
        countyIdx = -1;
        if ((countyIdx = s.indexOf("岛")) > -1)
            idxs[5] = countyIdx;
        Arrays.sort(idxs);
        for (int i = 0; i < idxs.length; i++) {
            int j = idxs[i];
            if (j > 0) {
                return j;
            }
        }

        return countyIdx;
    }
}

效果:

 

工具类②:


AddressResolutionUtil.java

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 身份证地址提取省市区工具类
 */
public class AddressResolutionUtil {

    /**
     * 根据身份证地址提取省市区工具类
     *
     * @param address
     * @return
     */
    public static List<Map<String, String>> addressResolution(String address) {
        String regex = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<district>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<detail>.*)";
        Matcher m = Pattern.compile(regex).matcher(address);
        String province = null, city = null, district = null, town = null, detail = null;
        List<Map<String, String>> table = new ArrayList<Map<String, String>>();
        Map<String, String> row = null;
        while (m.find()) {
            row = new LinkedHashMap<String, String>();
            province = m.group("province");
            row.put("province", province == null ? "" : province.trim());
            city = m.group("city");
            row.put("city", city == null ? "" : city.trim());
            district = m.group("district");
            row.put("district", district == null ? "" : district.trim());
            town = m.group("town");
            row.put("town", town == null ? "" : town.trim());
            detail = m.group("detail");
            row.put("detail", detail == null ? "" : detail.trim());
            table.add(row);
        }
        return table;
    }

    public static void main(String[] args) {
        System.out.println(addressResolution("内蒙古自治区兴安盟科尔沁右翼前旗"));
        System.out.println(addressResolution("山东省临沂市兰山区李官镇小李官村XXXXX号"));
    }
}

效果:


  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小目标青年

对你有帮助的话,谢谢你的打赏。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值