前端用法:

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

结果:

Java:

 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 }
12-17 07:11