用正则表达式切割详细地址中的省、市、区(例如:address=陕西省延安市宝塔区宝塔山街道宝塔山景区,切割之后为province=陕西省, city=延安市, county=宝塔区宝塔山街道宝塔山景)
将详细地址切割为省市区工具类(还要切割到镇、村庄参考博文:https://blog.csdn.net/superSubfn/article/details/80290491)
package com.camelot.attendance.util;
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;
/**
* <p>Description:[切割详细地址到省市区工具类,例如:将陕西省西安市高新区迈科大厦切割为:province=陕西省,city=西安市,region=x高新区]</p>
* Created on 2019年01月11日 14:54
*
* @author daihu
* @version 1.0
*/
public class AddressResolutionUtil {
public static Map<String, String> addressResolution(String address) {
//符合中国地区命名的正则表达式
String regex = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?<region>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?";
Matcher matcher = Pattern.compile(regex).matcher(address);
//省、市、区
String province = null, city = null, region = null;
Map<String, String> splitMap = null;
while (matcher.find()) {
splitMap = new LinkedHashMap<String, String>();
province = matcher.group("province");
splitMap.put("province", province == null ? "" : province.trim());
city = matcher.group("city");
splitMap.put("city", city == null ? "" : city.trim());
region = matcher.group("region");
splitMap.put("region", region == null ? "" : region.trim());
}
return splitMap;
}
public static void main(String[] args) {
System.out.println(addressResolution("陕西省延安市宝塔区麦克大厦"));
}
}
执行结果: