前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >地址字符串解析成对象

地址字符串解析成对象

作者头像
utopia
发布2023-03-20 17:25:23
6610
发布2023-03-20 17:25:23
举报
文章被收录于专栏:Utopia

需求

需要把地址字符串解析出来 比如广东省广州市白云区xxxx1洞,把省,市,区,详细地址解析出来。

代码语言:javascript
复制
public class DetailAddressUtil {

    /**
     * 省级地区不带省,市名称的地址
     */
    public static List<String> provinceList = Arrays.asList(new String[]{"内蒙古", "广西壮族", "重庆", "西藏", "宁夏", "新疆", "香港", "澳门"});

    /**
     * 根据地址字符串解析成对象
     *
     * @param address
     * @return
     */
    public static Location getLocationByAddress(String address) {
        Location location = new Location();
        String province = null, city = null, county = null, town = null, village = null, detailedAddress = null;
        if (StringUtils.isBlank(address)) {
            return location;
        }
        String substring = address.substring(0, 4);
        //香港新界单独处理
        if (substring.equals("香港新界")) {
            location.setProvince("香港");
            location.setCity("新界");
            String regex = "(?<county>[^县]+县|.+区|.+市|.+旗|.+委会|.+委员会|.+海域|.+岛)(?<village>.*)";
            Matcher m = Pattern.compile(regex).matcher(address.substring(4));
            while (m.find()) {
                county = m.group("county");
                location.setCounty(county == null ? "" : county.trim());
                village = m.group("village");
                detailedAddress = village == null ? "" : village.trim();
                location.setDetailedAddress(detailedAddress);
            }
            return location;
        }
        //判断标志
        Boolean isProvince = Boolean.TRUE;
        for (String s : provinceList) {
            if (substring.contains(s)) {
                location.setProvince(s);
                isProvince = Boolean.FALSE;
                break;
            }
        }
        //带省的城市
        if (isProvince) {
            String regex = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?岛|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+委会|.+委员会|.+海域|.+岛)(?<village>.*)";
            Matcher m = Pattern.compile(regex).matcher(address);
            while (m.find()) {
                province = m.group("province");
                location.setProvince(province == null ? "" : province.trim());
                city = m.group("city");
                location.setCity(city == null ? "" : city.trim());
                county = m.group("county");
                location.setCounty(county == null ? "" : county.trim());
                village = m.group("village");
                detailedAddress = village == null ? "" : village.trim();
                location.setDetailedAddress(detailedAddress);
            }
        } else {
            //不带省的城市
            String regex = "(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?岛|.*?行政区划|.*?县)(?<county>[^县]+县|.+区|.+市|.+旗|.+委会|.+委员会|.+海域|.+岛)(?<village>.*)";
            Matcher m = Pattern.compile(regex).matcher(address.substring(location.getProvince().length()));
            while (m.find()) {
                city = m.group("city");
                location.setCity(city == null ? "" : city.trim());
                county = m.group("county");
                location.setCounty(county == null ? "" : county.trim());
                village = m.group("village");
                detailedAddress = village == null ? "" : village.trim();
                location.setDetailedAddress(detailedAddress);
            }
        }
        return location;
    }


    @Data
    public static class Location {
        /**
         * 省
         */
        private String province;
        /**
         * 市
         */
        private String city;
        /**
         * 县
         */
        private String county;
        /**
         * 详细地址
         */
        private String detailedAddress;
    }
}

示例

入参
代码语言:javascript
复制
    public static void main(String[] args) {
        System.out.println(getLocationByAddress("内蒙古锡林郭勒盟正蓝旗xxx小区"));
        System.out.println(getLocationByAddress("澳门离岛圣方济各堂区xxx小区"));
        System.out.println(getLocationByAddress("香港新界北区xxx小区"));
        System.out.println(getLocationByAddress("北京市北京市东城区xxx小区"));
        System.out.println(getLocationByAddress("广东省阳江市阳西县xxx小区"));
    }
结果
代码语言:javascript
复制
DetailAddressUtil.Location(province=内蒙古, city=锡林郭勒盟, county=正蓝旗, detailedAddress=)
DetailAddressUtil.Location(province=澳门, city=离岛, county=圣方济各堂区, detailedAddress=)
DetailAddressUtil.Location(province=香港, city=新界, county=北区, detailedAddress=)
DetailAddressUtil.Location(province=北京市, city=北京市, county=东城区, detailedAddress=)
DetailAddressUtil.Location(province=广东省, city=阳江市, county=阳西县, detailedAddress=)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022/09/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 需求
  • 示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档