Java在逗号(,)上分割字符串,括号之间的除外() [英] Java split string on comma(,) except when between parenthesis ()

查看:88
本文介绍了Java在逗号(,)上分割字符串,括号之间的除外()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在逗号(,)上的java中拆分字符串,但是只要逗号(,)位于括号之间,就不应拆分.

I would like to split a string in java on a comma(,) but whenever the comma(,) is in between some parenthesis, it should not be split.

例如字符串:

"life, would, (last , if ), all"

应该产生:

-life
-would
-(last , if )
-all

当我使用时:

String text = "life, would, (last , if ), all"
text.split(",");

我最终将整个文本除以(最后一个if),我可以看到split使用了一个正则表达式,但是我似乎想不出如何使它完成工作.

I end up dividing the whole text even the (last , if ) I can see that split takes a regex but I can't seem to think of how to make it do the job.

推荐答案

您可以使用此模式-(不适用于嵌套括号)

you could use this pattern - (not for nested parenthesis)

,(?![^()]*\))

演示

,               # ","
(?!             # Negative Look-Ahead
  [^()]         # Character not in [()] Character Class
  *             # (zero or more)(greedy)
  \             # 
)               # End of Negative Look-Ahead
)

这篇关于Java在逗号(,)上分割字符串,括号之间的除外()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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