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 }
  • 相关阅读:
    以太坊公开拍卖智能合约案例
    部署智能合约
    ERC20 Token
    查看crontab运行状态
    php把excel数值格式转成日期格式
    PHP识别中文编码并自动转换为UTF-8
    python中文件内容替换
    mysqldump备份指定的数据
    linux设置服务器时间
    git常用命令
  • 原文地址:https://www.cnblogs.com/cjw-blogs/p/11981222.html
Copyright © 2011-2022 走看看