zoukankan      html  css  js  c++  java
  • 正则表达式小记

    最新很痴迷使用代码生成代码,因为有时候讨厌复制粘贴,现在发现grovvy和正则表达式对于自动生成代码真的非常方便。

    1. 正则表达式记录:

    public static List<String> getSubUtil(String soap,String rgex){
    List<String> list = new ArrayList<String>();
    Pattern pattern = Pattern.compile(rgex);// 匹配的模式
    Matcher m = pattern.matcher(soap);
    while (m.find()) {
    int i = 1;
    list.add(m.group(i));
    i++;
    }
    return list;
    }

    String reg = "‘(.*?)‘" //匹配字符串中在‘和‘之间的字符串
    List<String> result = getSubUtil(str, "put\("(.*?)","); //匹配字符串中在put("和",之间的字符串
    for (String s: result
    ) {
    System.out.println("<bean id="" + s + "" class="com.**" />");
    }

    2. JSON串groovy记录:
    对于非关系型数据,例如爬虫爬回来的数据,需要快速处理并拉平,存储下来,事例代码
    rawjson = new groovy.json.JsonSlurper().parseText(rawjson.content.unifieddata) //将json串转成LazyMap
    for (def baseTable : rawjson) {}  //遍历LazyMap

    if( vas instanceof List){ for(def val: vas)}
    else if(vas instanceof Map)
    vas.each{ def ele -> }

    3. groovy全局替换
    result?.pullDatas?.find {p->p.dataType == 'gxb_ecommerce_**'}.data.each {r->
    if(r?.time instanceof Long){
    r.time = new Date(r.time).format("yyyy-MM-dd HH:mm:ss.SSS")
    }
    }
    4. 分割 “|”
    String str = "abc|123"
    str.split("|") 期望得到结果abc和123 但是实际结果是a,b,c,1,2,3
    原因:因为|在正则表达式中代表OR,需要使用来转义它,因为在字符串中也是一个特殊字符,因此还需要\来转义它,正确结果是"\|"
    public static void main(String[] args) {
            String str = "434|3434";
    
            String[] arr = str.split("\|");
    
            System.out.println(arr);
        }
    欢迎关注Java流水账公众号
  • 相关阅读:
    【转】全文检索引擎Sphinx配置文件详细介绍
    【转】构建不依赖于cookie的手机端用户登录机制
    Sphinx在window下的初步安装和配置
    Zend Framework 在.htaccess中修改RewriteRule实现url重写
    做后台的程序猿的一点心得
    [Leetcode 75] 96 Unique Binary Search Trees
    [Leetcode 74] 92 Restore IP Addresses
    [Leetcode 73] 92 Reverse Linked List II
    [Leetcode 72] 91 Decode Ways
    [Leetcode 71] 86 Partition List
  • 原文地址:https://www.cnblogs.com/guofu-angela/p/9284040.html
Copyright © 2011-2022 走看看