zoukankan      html  css  js  c++  java
  • 【】正则表达式示例

    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(下划线分割)
        }
    }
    
    
    
  • 相关阅读:
    IP是什么 DNS 域名与IP有什么不同
    空间、域名与IP之间的关系?
    杨学明老师为深圳某上市企业提供《软件测试技术》内训服务!
    共创力与某上市企业合作的第三期咨询项目正式启动!
    2017.7.28~29,热烈庆祝杨学明老师《研发项目管理》杭州公开课成功举办!
    2017年7月22日~23日,深圳市共创力为某上市企业提供整机设计工程内训服务!
    深圳市共创力咨询第二期“总裁直通车”成功举办!
    阿里巴巴产品需求工程师的三个层次
    共创力咨询推出“总裁直通车”服务!
    2017年7月7日~8日,杨学明老师为深圳蛇口某企业内训课程服务!
  • 原文地址:https://www.cnblogs.com/ssslinppp/p/7383957.html
Copyright © 2011-2022 走看看