是否有简单的正则表达式将Java String List转换为Oracle IN条件? [英] Is there simple regex to convert Java String List to Oracle IN condition?
问题描述
例如:
有一个String列表(ArrayList< String>) shopList
,以及像这样输出toString()
There is a List of String (ArrayList < String >) shopList
, and output toString() like this
[bread, sugar, butter]
现在我需要将此List转换为Oracle IN条件,以便它变为:
now I need to convert this List to an Oracle IN condition, so that it become:
('bread', 'sugar', 'butter')
我使用
String shopListString= "(".concat(shopList.toString().replaceAll("[\\s\\[\\]]", "'")).concat(")");
但首先,我尝试避免连续但单独使用正则表达式,也只是输出后面哪个是错误的:
but first, I try to avoid concat but use regex alone, also it just output following which is wrong:
('bread,'sugar,'butter')
只有最后一项带有开头'
only last item comes with beginning '
推荐答案
基于@Keppil的方法很简单:
There is a simple way base on @Keppil:
"('"+ StringUtils.join(myList, "','") + "')"
这是原始答案用法 group
要替换的变量:
This is origin answer use group
variable to replace:
"("+ StringUtils.join(myList, ",").replaceAll("(\\w+)", "'$1'") + ")"
您可以使用 StringUtils
join
string and surround group
替换单词的变量。
You can use StringUtils
join
string and surround group
variable to replace the word.
这篇关于是否有简单的正则表达式将Java String List转换为Oracle IN条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!