省市区地区分割,注意地址里面包括两个区的(比如有:市区和小区)

public class AddressResolutionUtil{

  /**

    * 解析地址

    * @author lin

    * @param address

    * @return

    */

    public static List> addressResolution(String address){

        String regex="(?[^省]+自治区|.*?省|.*?行政区|.*?市)(?[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?[^县]+县|.+?区|.+市|.+旗|.+海域|.+岛)?(?[^区]+区|.+镇)?(?.*)";

Matcher m=Pattern.compile(regex).matcher(address);

String province=null,city=null,county=null,town=null,village=null;

List> table=new ArrayList>();

Map row=null;

while(m.find()){

            row =new LinkedHashMap();

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;

}

    public static void main(String[] args) {

        //最初的

        //String regex="(?[^省]+自治区|.*?省|.*?行政区|.*?市)(?[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?[^区]+区|.+镇)?(?.*)";

        //除了省市,其他都放详细地址里面

        //String regex="(?[^省]+自治区|.*?省|.*?行政区|.*?市)(?[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?[^县]+县|.+[.区]+区|.+市|.+旗|.+海域|.+岛)?(?[.区]+区|.+镇)?(?.*)";

        String regex="(?[^省]+自治区|.*?省|.*?行政区|.*?市)(?[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)(?[^县]+县|.+?区|.+市|.+旗|.+海域|.+岛)?(?[^区]+区|.+镇)?(?.*)";

Matcher m=Pattern.compile(regex).matcher("湖北省武汉市武昌区徐东大街岳家嘴电信英特小区9栋3单元601室");

String province=null,city=null,county=null,town=null,village=null;

List> table=new ArrayList>();

Map row=null;

while(m.find()){

            row =new LinkedHashMap();

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);

}

        System.out.println(table);

}

}

你可能感兴趣的:(省市区地区分割,注意地址里面包括两个区的(比如有:市区和小区))