package cn.iocoder.yudao.module.rescue.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class AddressSplitter {
    public static List<String> splitAddress(String address) {
        List<String> res =new ArrayList<>();
        // 定义省、市、区的正则表达式
        String provinceRegex = "(?<=省|自治区|特别行政区).+?(?=(市|地区|州))";
        String cityRegex = "(?<=市).+?(?=(区|县|市|自治州))";
        String districtRegex = "(?<=(市|区|县|镇)).+?(?=(街道|乡|村))";

        // 初始化省、市、区
        String province = "";
        String city = "";
        String district = "";

        // 匹配省份
        String[] provinceArray = StringUtils.splitByWholeSeparatorPreserveAllTokens(address, "省");
        if (provinceArray.length > 1) {
            province = provinceArray[0] + "省";
            address = provinceArray[1];
        }

        // 匹配城市
        String[] cityArray = StringUtils.splitByWholeSeparatorPreserveAllTokens(address, "市");
        if (cityArray.length > 1) {
            city = cityArray[0] + "市";
            address = cityArray[1];
        }

        // 匹配区县
        String[] districtArray = StringUtils.splitByWholeSeparatorPreserveAllTokens(address, "区");
        if (districtArray.length > 1) {
            district = districtArray[0] + "区";
        }
        res.add(province);
        res.add(city);
        res.add(district);
        return res;
    }
}
