我有一个长字符串,只要逗号不在方括号或括号内,我想通过用逗号分隔它来将其设置为数组。我尝试了几种变化,但没有得到我正在寻找的......
示例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
答案 0 :(得分:6)
您可以将以下正则表达式与全局标记一起使用。
,(?![^\(\[]*[\]\)])