繁体   English   中英

如果不在方括号或括号内,请用逗号分隔字符串

[英]Split string by comma if not within square brackets or parentheses

我有一个长字符串,只要逗号不在方括号或括号内,我想通过用逗号将其分割成一个数组。 我尝试了几种变体,但未完全得到我想要的...

范例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

您可以将以下正则表达式与全局标志一起使用。

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

这是一个演示 它受到https://stackoverflow.com/a/9030062/1630604的启发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM