/**
* 匹配地址并提取省市区(县/市)
*
* @param address - 完整地址
* @return 返回配置的省市区(县 / 市),未匹配则返回 null
*/
public static String matchDistrict(String address) {
if (StringUtils.isEmpty(address)) {
return null;
}
// 匹配“市”字出现的次数,因为有县级市的情况,需要提取到市;否则,提取到县
long count = address.chars().filter(ch -> ch == '市').count();
String regExp = count > 1 ? "\\S+(?<=[区县市])" : "\\S+(?<=[区县])";
// 匹配地址中的 区/县(或县级市)
Matcher mc = Pattern.compile(regExp).matcher(address);
if (mc.find()) {
return mc.group();
}
return null;
}
改进版
private final static Pattern ADDRESS_PATTERN = Pattern.compile("(?<province>(\\S+省|自治区)|北京市|上海市|天津市|重庆市)?(?<city>\\S+市|地区)?(?<district>\\S+[市区县])?");
/**
* 提取地址的省市区(县)
*/
public static String lookBehindDistrict(String address) {
Matcher matcher = ADDRESS_PATTERN.matcher(address);
if (matcher.find()) {
String province = matcher.group("province");
String city = matcher.group("city");
String district = matcher.group("district");
return concatNonNullValues(province, city, district);
}
return null;
}
/**
* 拼接非空字符串数组
*/
public static String concatNonNullValues(String... str) {
StringBuilder sb = new StringBuilder();
for (String s : str) {
if (StringUtils.isNotEmpty(s)) {
sb.append(s);
}
}
return sb.toString();
}
正则可参考: