正則表達式 分割地址 獲取省市區詳細地址

前端用法:前端

 

1 let regex = "(?<province>[^省]+省|.+自治區)(?<city>[^自治州]+自治州|[^市]+市|[^盟]+盟|[^地區]+地區|.+區劃)(?<county>[^市]+市|[^縣]+縣|[^旗]+旗|.+區)?(?<town>[^區]+區|.+鎮)?(?<village>.*)";
2 let address = "安徽省淮南市大通區大通街道某某某";
3 console.log(address.match(regex));

結果:spa

Java:code

 1 public class AddressResolution {
 2  
 3     /**
 4      * 解析地址
 5      * @author cjw
 6      * @param address
 7      * @return
 8      */
 9     public static List<Map<String,String>> addressResolution(String address){
10         String regex="(?<province>[^省]+省|.+自治區)(?<city>[^自治州]+自治州|[^市]+市|[^盟]+盟|[^地區]+地區|.+區劃)(?<county>[^市]+市|[^縣]+縣|[^旗]+旗|.+區)?(?<town>[^區]+區|.+鎮)?(?<village>.*)";
11         Matcher m=Pattern.compile(regex).matcher(address);
12         String province=null,city=null,county=null,town=null,village=null;
13         List<Map<String,String>> list=new ArrayList<Map<String,String>>();
14         Map<String,String> row=null;
15         while(m.find()){
16             row=new LinkedHashMap<String,String>();
17             province=m.group("province");
18             row.put("province", province==null?"":province.trim());
19             city=m.group("city");
20             row.put("city", city==null?"":city.trim());
21             county=m.group("county");
22             row.put("county", county==null?"":county.trim());
23             town=m.group("town");
24             row.put("town", town==null?"":town.trim());
25             village=m.group("village");
26             row.put("village", village==null?"":village.trim());
27             list.add(row);
28         }
29         return list;
30     }
31 //    public static void main(String[] args) {
32 //        System.out.println("地址是:" + addressResolution("寧夏回族自治區銀川市靈武市"));
33 //    }
34 }
相關文章
相關標籤/搜索