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'
posted on 2017-07-07 21:15 姜小嫌 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/jiangxiaoxian/p/7134201.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值