zoukankan      html  css  js  c++  java
  • java 正则 贪婪匹配 匹配sql语句中的引号内容

    public class Demo {
        public static void main(String[] args) {
            String sql1 = "use test;select * from default.abc where dt='abc;faf;fff' and ct="2012;43" ; ";
            sql1 = "select * from aaa where dt= '20 ;12; 34;3' AND name='fafae; fa ; a'";
            //配置“” 或 ‘’ 里的内容
                Pattern p1 = Pattern.compile("'.*'|".*"");//贪婪匹配
    
                Matcher m1 = p1.matcher(sql1);
                String replace1 = null;
                while (m1.find()) {
                // 贪婪匹配 group= '20 ;12; 34;3' AND name='fafae; fa ; a'
                    String group = m1.group();
                    if (group.contains(";")) {
                        replace1 = sql1.replace(group, "'PLACEHOLDER'");
                        sql1 = replace1;
                    }
                }
            System.out.println("贪婪匹配" + sql1);
    
            String sql2 = "select * from aaa where dt= '20 ;12; 34;3' AND name='fafae; fa ; a'";
            //配置“” 或 ‘’ 里的内容
            Pattern p2 = Pattern.compile("'.*?'|".*?"");//非贪婪匹配
            Matcher m2 = p2.matcher(sql2);
            String replace2 = null;
            while (m2.find()) {
            // 第一次匹配  group   '20 ;12; 34;3'
            // 第二次匹配  group   'fafae; fa ; a'
                String group = m2.group();
                if (group.contains(";")) {
                    replace2 = sql2.replace(group, "'PLACEHOLDER'");
                    sql2 = replace2;
                }
            }
            System.out.println("非贪婪匹配" + sql2);
    
        }
    }
    
    
    结果:
    贪婪匹配  select * from aaa where dt= 'PLACEHOLDER'
    非贪婪匹配select * from aaa where dt= 'PLACEHOLDER' AND name='PLACEHOLDER'
    
    
  • 相关阅读:
    Docker安装ngnix进行挂载
    Linux上传下载小工具
    uniapp——原生导航栏
    uniapp——scroll-view组件隐藏滚动条
    边框阴影——box-shadow
    uniapp-监听自定义跳转
    uniapp整屏滑动
    用伪类写圆点css
    Vue——生命周期
    uniapp多选按钮
  • 原文地址:https://www.cnblogs.com/jiangxiaoxian/p/7134201.html
Copyright © 2011-2022 走看看