java中replaceAll替换圆括号实例代码

作者:天狼1222 时间:2023-09-30 16:29:54 

前言

在手写sql的时候,根据参数处理查询条件.

select * from staff where 1 = 1 and staff_id in ($staffIds)
and staff_name in ($staffNames)

比如staffId为空,需要把staff_id in ($staffIds) 候设置为true,staffName正常赋值

replace替换圆括号

public static void main(String[] args) {
   String sqlStr = "select * from staff where 1 = 1 and staff_id in ($staffIds) " +
           "and staff_name in ($staffNames)";
   String replaceEmpty = "staff_id in ($staffIds)";
   String replaceSql = sqlStr.replace(replaceEmpty, "true");
   System.out.println(replaceSql);
}

结果:

select * from staff where 1 = 1 and true and staff_name in ($staffNames)

直接用replace就可以了。

replaceAll替换圆括号

如果是想用replaceAll呢?

public static void main(String[] args) {
   String sqlStr = "select * from staff where 1 = 1 and staff_id in ($staffIds) " +
           "and staff_name in ($staffNames)";
   String replaceEmpty = "staff_id in ($staffIds)";
   String replaceAllSql =  sqlStr.replaceAll(Matcher.quoteReplacement(replaceEmpty), "true");
   System.out.println(replaceAllSql);
}

结果:

select * from staff where 1 = 1 and staff_id in ($staffIds)
and staff_name in ($staffNames)

没有替换成功,为什么呢?

看replaceAll的方法:

public String replaceAll(String regex, String replacement) {
   return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}

regex 对应的是正则的内容,因此要对圆括号进行转移下:

String replaceAllSql =  replaceEmpty =  replaceEmpty.replaceAll("\\(", "[( )]").replaceAll("\\)", "[( )]");

 替换前,对圆括号进行转义

public static void main(String[] args) {
   String sqlStr = "select * from staff where 1 = 1 and staff_id in ($staffIds) " +
           "and staff_name in ($staffNames)";
   String replaceEmpty = "staff_id in ($staffIds)";
   replaceEmpty =  replaceEmpty.replaceAll("\\(", "[( )]").replaceAll("\\)", "[( )]");
   String replaceAllSql =  sqlStr.replaceAll(Matcher.quoteReplacement(replaceEmpty), "true");
   System.out.println(replaceAllSql);
}

结果:

select * from staff where 1 = 1 and true and staff_name in ($staffNames)

替换成功。

补充:Java 利用replaceAll 替换中括号

Java的replaceAll函数默认是不能替换中括号的,例如想替换[b]到<b>,结果却就变成[<b>]

解决方案就是首先利用正则表达式替换中括号,然后再替换中括号内的内容:

infos = infos.replaceAll("[\\[\\]]","");

不过后来又查询了下资料,发现中括号在java中居然是特殊字符,一对中括号里的内容是一组正则表达式。所以如果打算让[b]-><b>,只要如下写法:

infos = infos.replaceAll("\\[b\\]","<b>");

总结:

字符替换的时候,优先考虑使用replace,多个时候,也是生效的。如果要使用replace的话,使用要注意特殊字符的处理。或者自己写正则进行处理。

优化: 《整体替换sql》

来源:https://blog.csdn.net/qq_35461948/article/details/123126296

标签:replaceall,替换,圆括号
0
投稿

猜你喜欢

  • 详解Kotlin:forEach也能break和continue

    2022-05-03 01:24:10
  • java多线程的同步方法实例代码

    2022-02-16 19:30:47
  • Vs2022环境下安装低版本.net framework的实现步骤

    2023-07-04 02:58:12
  • Android 调用系统相机拍摄获取照片的两种方法实现实例

    2022-01-19 21:22:23
  • mybatis-plus自动填充插入更新时间有8小时时差

    2021-07-26 07:18:40
  • C# readnodefile()不能读取带有文件名为汉字的osg文件解决方法

    2021-11-08 07:41:00
  • 利用@Value注解为bean的属性赋值方法总结

    2023-10-15 19:54:06
  • 解决mybatis-plus3.1.1版本使用lambda表达式查询报错的方法

    2022-03-19 03:55:09
  • 在IntelliJ IDEA中使用gulp的方法步骤(图文)

    2022-10-12 06:29:08
  • mybatis源码解读-Java中executor包的语句处理功能

    2023-09-03 06:34:16
  • Java开发岗位面试被问到反射怎么办

    2023-01-07 00:09:12
  • java synchronized 锁机制原理详解

    2021-10-15 05:29:47
  • Android音频开发之音频采集的实现示例

    2022-04-12 16:28:31
  • Android中的图片优化完全指南

    2021-09-06 04:45:27
  • springboot整合mybatis的超详细过程(配置模式+注解模式)

    2023-10-03 09:54:03
  • Java操作redis实现增删查改功能的方法示例

    2022-01-04 07:24:24
  • Android自定义TitleView标题开发实例

    2023-09-05 18:21:41
  • .net与javascript脚本的交互方法总结

    2021-09-11 09:21:32
  • 详解Java中日志跟踪的简单实现

    2023-03-28 00:18:48
  • Unity游戏开发实现场景切换示例

    2022-12-12 16:06:20
  • asp之家 软件编程 m.aspxhome.com