【问题标题】:Split string by comma if not within square brackets or parentheses如果不在方括号或圆括号内,则用逗号分割字符串
【发布时间】:2016-08-03 15:26:00
【问题描述】:

我有一个长字符串,只要逗号不在方括号或圆括号内,我想通过用逗号分割它来将它设置为一个数组。我尝试了几种变体,但没有得到我想要的……

示例 1:

Harry Potter, Hermione, (Severus, Snape)
Returns:
Harry Potter
Hermione
Severus, Snape

示例 2:

Harry Potter, [and, the chamber, of secrets], Hermione, (Olivanders, Wands)
Returns:
Harry Potter
and, the chamber, of secrets
Hermione
Olivanders, Wands

【问题讨论】:

  • @Marc - 我看到了这个问题,但我仍然不清楚如何解释我正在寻找的这两个条件。
  • 重新打开,因为这个问题是关于括号和括号:完整的答案更难。
  • 不错的地方,但恕我直言,值得特别回答一下,尽管您找到的答案可能足以满足 OP。

标签: java regex


【解决方案1】:

您可以使用以下带有全局标志的正则表达式。

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

这是demo。 它的灵感来自https://stackoverflow.com/a/9030062/1630604。

【讨论】:

    猜你喜欢
    • 2021-06-12
    • 1970-01-01
    • 2017-04-07
    • 2010-12-11
    • 2020-04-26
    • 2016-01-12
    • 2019-01-23
    • 2013-02-20
    相关资源
    最近更新 更多