Java-提取地址中的省市区,兼容XX区XX小区等地址中出现多个市和区的问题
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class AddressCuttingUtil {
public static String addressCutting(String address) {
if (address.startsWith("北京市") || address.startsWith("天津市") || address.startsWith("上海市") || address.startsWith("重庆市")) {
address = address.substring(0, 3) + "市辖区" + address.substring(3);
}
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;
while(m.find()){
province = m.group("province");
if (province.equals("北京市") || province.equals("天津市") || province.equals("上海市") || province.equals("重庆市")) {
city = province;
county = m.group("city");
if (county.split("区").length > 1) {
town = county.substring(county.indexOf("区") + 1);
county = county.substring(0, county.indexOf("区") + 1);
if (town.contains("区")) {
town = town.substring(county.indexOf("区") + 1);
}
} else {
county = m.group("county");
if (county != null && county.split("区").length > 1) {
town = county.substring(county.indexOf("区") + 1);
county = county.substring(0, county.indexOf("区") + 1);
}
}
} else {
city = m.group("city");
county = m.group("county");
if (county != null && !"".equals(county)) {
if (county.split("市").length > 1 && county.indexOf("市") < 5) {
town = county;
county = county.substring(0, county.indexOf("市") + 1);
town = town.substring(county.indexOf("市") + 1);
}
if (county.split("旗").length > 1) {
town = county;
county = county.substring(0, county.indexOf("旗") + 1);
town = town.substring(county.indexOf("旗") + 1);
}
if (county.split("海域").length > 1) {
town = county;
county = county.substring(0, county.indexOf("海域") + 2);
town = town.substring(county.indexOf("海域") + 2);
}
if (county.split("区").length > 1) {
town = county;
county = county.substring(0, county.indexOf("区") + 1);
town = town.substring(county.indexOf("区") + 1);
}
}
}
if (province != null && !"".equals(province)) {
province = province + "-";
}
if (city != null && !"".equals(city)) {
city = city + "-";
}
if (county != null && !"".equals(county)) {
county = county + "-";
}
if (m.group("town") != null) {
town += m.group("town");
}
if ((county == null || "".equals(county)) && town != null && !"".equals(town)) {
town = town + "-";
}
village=m.group("village");
}
String newMachineAdress = province + city + county + town + village;
if (newMachineAdress != null && !"".equals(newMachineAdress)) {
newMachineAdress = newMachineAdress.replaceAll("null", "");
}
if (newMachineAdress == null || "".equals(newMachineAdress)) {
newMachineAdress = address;
}
return newMachineAdress;
}
public static void main(String[] args) {
System.out.println(addressCutting("北京市朝阳区幸福小区101"));
System.out.println(addressCutting("上海市黄浦区鑫浩壹都A座10楼"));
System.out.println(addressCutting("广东省深圳市罗湖区幸福小区XX区"));
System.out.println(addressCutting("湖南省长沙市某某县幸福小区"));
System.out.println(addressCutting("广东省深圳市南山区桃源街道桃源社区高发西路XX号深圳市XX公司X层A区"));
System.out.println(addressCutting("广东省深圳市南山区北环路第五工业区XX楼三楼A区"));
System.out.println(addressCutting("天津市和平区和平区南市盒华安大街交口XX有限公司"));
System.out.println(addressCutting("天津市和平区南市盒华安大街交口XX有限公司"));
System.out.println(addressCutting("湖北省武汉市洪山区XX街道"));
System.out.println(addressCutting("湖北省恩施土家族苗族自治州恩施市XX区"));
System.out.println(addressCutting("内蒙古自治区兴安盟科尔沁右翼前旗XX区"));
System.out.println(addressCutting("*自治区日喀则地区日喀则市XX区"));
System.out.println(addressCutting("海南省省直辖县级行政单位中沙群岛的岛礁及其海域XX区"));
}
}