ssslinppp

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
package com.chinamobile.epic.common.regex;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 */
public class TargetParamRegexMatches {
    private static final Logger logger = LoggerFactory.getLogger(TargetParamRegexMatches.class);

    /**
     * 从 sourceString 中匹配出 params 中的值
     *
     * @param pattern      正则表达式
     * @param sourceString 待匹配的字符串
     * @param params       参数列表
     * @return
     */
    public static Map<String, String> findParams(String pattern, String sourceString, List<String> params) {
        if (CollectionUtils.isEmpty(params)) {
            logger.warn("params can not be null or emtpy");
            return Maps.newConcurrentMap();
        }

        Map<String, String> mapResult = Maps.newConcurrentMap();

        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);

        // 现在创建 matcher 对象
        Matcher m = r.matcher(sourceString);
        if (m.find()) {
            for (String param : params) {
                try {
                    String value = m.group(param);
                    if (!Strings.isNullOrEmpty(value)) {
                        mapResult.put(param, value);
                    }
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
        }

        return mapResult;
    }


    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        List<String> params = Lists.newArrayList();
        params.add("type");
        params.add("hostId");

        //-----------start 测试匹配 uuid(32-36位)
        String line1 = "summarize(clm.pm.xxx.yyy.uuu.zzz.0193f1b3-7bcc-4374-8546-8e87b7276003.agg.cpu.percent-avg.system,'10min','avg'," +
                "true)";
        String line2 = "summarize(clm.pm.0193f1b3-7bcc-4374-8546-8e87b7276003.agg.cpu.percent-avg.system,'10min','avg',true)";
//        String pattern = "^.*(?<hostId>\\w{8}(?:-\\w{4}){3}-\\w{12}?)";  // OK: 匹配 uuid(32~36位)
        String pattern = "^.*clm\\.(?<type>\\w+\\.*.*)\\.(?<hostId>\\w{8}(?:-\\w{4}){3}-\\w{12}?)"; //OK

        System.out.println("-----line1--------");
        System.out.println(findParams(pattern, line1, params).toString());
        System.out.println("-----line2--------");
        System.out.println(findParams(pattern, line2, params).toString());
        /* outPut
        -----line1--------
        {hostId=0193f1b3-7bcc-4374-8546-8e87b7276003, type=pm.xxx.yyy.uuu.zzz}
        -----line2--------
        {hostId=0193f1b3-7bcc-4374-8546-8e87b7276003, type=pm}
         */
        //-----------end 测试匹配 uuid(32-36位)

        //-----------start 测试匹配 ip(下划线分割)
        System.out.println("----------------IP------------");
        String lineIp = "summarize(clm.pm.xxx.yyy.uuu.zzz.10_144_202_141.agg.cpu.percent-avg.system,'10min','avg'," +
                "true)";
        String patternIp = "^.*clm\\.(?<type>\\w+\\.*.*)\\.(?<hostId>\\d{0,3}_\\d{0,3}_\\d{0,3}_\\d{0,3})";
        System.out.println(findParams(patternIp, lineIp, params).toString());
        //-----------start 测试匹配 ip(下划线分割)
    }
}


posted on 2017-08-17 19:27  ssslinppp  阅读(194)  评论(0编辑  收藏  举报