• 正则表达式小记


    最新很痴迷使用代码生成代码,因为有时候讨厌复制粘贴,现在发现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流水账公众号
  • 相关阅读:
    Asp.Net Core 2.0 之旅---在Ubuntu上部署WEB应用程序
    xml对象序列化
    txt文本文件记录日志
    HttpGet HttpPost
    c# MD5
    10位时间戳转为C#格式时间
    树莓派上运行.net core 2.0程序
    c# 解析json
    小程序与后台数据交互时出现乱码时
    小程序template怎样渲染页面的
  • 原文地址:https://www.cnblogs.com/guofu-angela/p/9284040.html
Copyright © 2020-2023  润新知