java replaceAll替换圆括号
在手写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)
替换成功。
总结:
字符替换的时候,优先考虑使用replace,多个时候,也是生效的。如果要使用replace的话,使用要注意特殊字符的处理。或者自己写正则进行处理。
优化: 《整体替换sql》
java replaceAll替换圆括号相关推荐
- java 查找 替换_java 查找、替换
在工作中经常用到的查找替换方式是String的replace.replaceAll.不过配合使用Pattern.Matcher.StringBuffer可以实现更灵活的替换.代码如下,重点看一下&qu ...
- java replaceAll() 方法要用 4 个反斜杠,表示一个反斜杠,该怎么解决
2019独角兽企业重金招聘Python工程师标准>>> java replaceAll() 方法要用 4 个反斜杠,表示一个反斜杠 java replaceAll() 方法要用 4 ...
- java正则表达式替换特殊字符_使用正则表达式替换报表名称中的特殊字符(推荐)...
正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表通常被用来检索.替换那些符合某个模式(规则)的文 ...
- Java中替换字符串的方法
Java中替换字符串可以用replace和replaceAll这两种,区别是, 1. replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSeq ...
- java replaceall 多行_Java replaceAll() 方法 - Break易站
Java replaceAll() 方法 Java String类 replaceAll() 方法使用给定的参数 replacement 替换字符串所有匹配给定的正则表达式的子字符串. 语法 publ ...
- java字符替换函数示例_Java字符串替换函数replace()用法解析
Java字符串替换函数replace()用法解析 这篇文章主要介绍了Java字符串替换函数replace()用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的 ...
- java集合替换集合_Java集合–您必须知道的13件事
java集合替换集合 Java Collections Framework is one of the core parts of the Java programming language. Col ...
- JAVA 字符串替换占位符
JAVA 字符串替换占位符 1 MessageFormat.format() code public static void main(String[] args) {String a= " ...
- Java 利用replaceAll 替换中括号
Java的replaceAll函数默认是不能替换中括号的,例如想替换[b]到<b>,结果却就变成[<b>] 解决方案就是首先利用正则表达式替换中括号,然后再替换中括号内的内容: ...
最新文章
- CRM User Status profile中Business Transaction字段的用途
- 【解析】工业机器人中的各类传感器技术应用
- Ubuntu 取消 Apache及MySQL等自启动
- 论文笔记:Forecasting at Scale(Prophet)
- PHP-代码审计-文件读取(下载)
- access vba 用recordset读取表中数据的简单方法
- onvif device manager 找不到ipc_Qt音视频开发32-Onvif网络设置
- fwoa中workflow_requestbase表currentnodetype字段含义及查看归档流程的requestid
- 前端设计——音乐盒子
- Mysql- --DQl语句(select数据查询语言,多表查询,View试图)linux常用(重点)
- redis 的6种过期策略
- python小游戏——俄罗斯方块
- parrot linux iso下载,Parrot Security OS 4.0发布下载,面向安全的操作系统
- 超声的pacs系统和dicom服务器,PACS系统中DICOM/HL7网关与DICOM服务器的研究与实现
- 不使用采集卡,实现相机手机多机位直播
- 审稿意见回复信英文模板和语料总结
- PTA甲级模拟第九弹:1114-1117
- JS中操作<select>标签选的值
- python end用法_python中end的使用方法
- python plt图片保存emf类型_如何获得emf/wmf格式的matplotlib图形?