zoukankan      html  css  js  c++  java
  • 正则表达式 分割地址 获取省市区详细地址

    前端用法:

    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 }
  • 相关阅读:
    Cnic.SafeNativeMethods
    KnockOut文档--模板绑定
    luoguP1120 小木棍 [数据加强版]
    luoguP1951 收费站_NOI导刊2009提高(2)
    luoguP1821 [USACO07FEB]银牛派对Silver Cow Party
    luoguP2991 [USACO10OPEN]水滑梯Water Slides
    luoguP4198 楼房重建
    (数位dp)吉利数字 区间k大
    数字游戏
    Amount of Degrees
  • 原文地址:https://www.cnblogs.com/cjw-blogs/p/11981222.html
Copyright © 2011-2022 走看看