关键词搜索

源码搜索 ×
×

springboot Java正则表达式循环匹配字符串

发布2021-01-20浏览1638次

详情内容

public static void main(String[] args) {
    /**
     * String str = "[Part 'name=1234567']";
     */
    String targetStr = "where 1=1 [and user_name like '%$user_name$%']  [and company_name like '%$company_name$%']";
    String[] split = targetStr.split("]");
    System.out.println("split==== " + split);
    String patternStr = "^[\\s\\S]{0,}\\[\\s{0,}and\\s{0,}[\\d\\W\\w]{1,}\\s{0,}[\\w]{1,}\\s{0,}\\W{0,}(\\$.+?\\$)\\W{0,}\\s{0,}\\s{0,}$";
 
    // 正则表达式
    Pattern pattern = Pattern.compile(patternStr);
    // 参数:要匹配的目标字符串
    Map<String,String> map = new HashMap<String,String>();
    map.put("$user_name$","user_value");
    map.put("$company_name$","company_value");
    map.put("$school_name$","$school_name$");
    /*int groupCount = matcher.groupCount();
    System.out.println("groupCount==== " + groupCount);*/
    for (int i = 0;i<=split.length-1;i++) {
        Matcher matcher = pattern.matcher(split[i]);
        System.out.println("split[i]==== " + split[i]);
        boolean b = matcher.find();
        if (b) {
            String key = matcher.group(1);
            for (Map.Entry<String,String> m : map.entrySet()){
                String mapKey = m.getKey();
                if (key.equalsIgnoreCase(mapKey)) {
                    System.out.println("key==== " + key );
                    /*String bigGroup = matcher.group();
                    System.out.println("bigGroup==== " + bigGroup);*/
                    targetStr = targetStr.replace(key,m.getValue());
                   // System.out.println("targetStr after replace innner==== " + targetStr);
                }
            }
 
        }
      //  System.out.println("targetStr after replace outer==== " + targetStr);
    }
    System.out.println("targetStr after replace final==== " + targetStr);
}

    相关技术文章

    点击QQ咨询
    开通会员
    返回顶部
    ×
    微信扫码支付
    微信扫码支付
    确定支付下载
    请使用微信描二维码支付
    ×

    提示信息

    ×

    选择支付方式

    • 微信支付
    • 支付宝付款
    确定支付下载