在逗号上拆分字符串,但在括号内不是逗号 [英] split string on comma, but not commas inside parenthesis

查看:104
本文介绍了在逗号上拆分字符串,但在括号内不是逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个关于Java IO的任务,我需要逐行读取文件并将这些数据保存到数据库中,但是现在我遇到了问题。

Now I have a task about Java IO , I need to read file line by line and save this data into database, But Now I got a problem.

"desk","12","15","(small,median,large)"

我想用 string.split(,)来拆分,并将数据保存到每一列。但是我发现()中的数据也被拆分了,我不想拆分(小,中,大),我想保持这种完整性。我怎样才能做到这一点?我知道我可以使用regualr表达式,但我真的不知道怎么做?

I want to use string.split(",") to split by , and save data into each column. But I found that the data in the () has also been split, I do not want to split (small, median, large) and I want to keep this integrity. How can I do that? I know I can use regualr expression , but I really do not know how to do it?

推荐答案

(\(.+?\)|\w+)

上面的代码匹配下面的结果将允许一些更灵活的解决方案,其他一些发布的。正则表达式的语法在此页面的另一个答案中,只需使用此正则表达式

the code above matches the result below this will allow for a more flexible solution that some of the other posted ones. The syntax for the regular expression is in another answer on this page just use this regular expression instead

这篇关于在逗号上拆分字符串,但在括号内不是逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆