正则表达式匹配提取省市区

前言

本文主要介绍通过正则表达式匹配提取省市区,有完整的代码示例和详细的注释。

package com.importData;

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;

/**
 * @author xiang.wei
 * @create 2018/3/1 16:44
 */
public class OriginTest {
    /**
     * 省
     */
    private static final String PROVINCE = "省";
    /**
     * 市
     */
    private static final String CITY = "市";
    /**
     * 区
     */
    private static final String REGION_1 = "区";
    /**
     * 县
     */
    private static final String REGION_2 = "县";

    public static void main(String[] args) {
//        XX省XX市XX区;XX省XX市XX市;XX省XX市XX县;XX市XX区;XX市XX县
//        1.浙江省杭州市滨江区
//        2.上海上海市金山区
//        3.浙江省台州市玉环县
//        4.湖北省潜江市潜江经济开发区
        String[] placeList = new String[]{"浙江省杭州市滨江区","上海上海市金山区","浙江省台州市玉环县","湖北省潜江市潜江经济开发区","湖北省潜江市江汉石油管理局","湖北省天门市马湾镇"};
        for (String place : placeList) {
            System.out.println(addressResolution(place));
        }
    }

    /**
     * 解析地址
     * @param address
     * @return
     */
    public static List<Map<String,String>> addressResolution(String address){
        String regex="((?<province>[^省]+省|.+自治区)|上海|北京|天津|重庆)(?<city>[^市]+市|.+自治州)(?<county>[^县]+县|.+区|.+镇|.+局)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
        Matcher m=Pattern.compile(regex).matcher(address);
        String province=null,city=null,county=null,town=null,village=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());
            county=m.group("county");
            row.put("county", county==null?"":county.trim());
            town=m.group("town");
            row.put("town", town==null?"":town.trim());
            village=m.group("village");
            row.put("village", village==null?"":village.trim());
            table.add(row);
        }
        return table;
    }

}

测试结果:

在这里插入图片描述






作者:码农飞哥
微信公众号:码农飞哥