我有一个长字符串,只要逗号不在方括号或括号内,我想通过用逗号分隔它来将其设置为数组.我尝试了几种变化,但没有得到我正在寻找的东西......
例1:
Harry Potter, Hermione, (Severus, Snape)
Returns:
Harry Potter
Hermione
Severus, Snape
Run Code Online (Sandbox Code Playgroud)
例2:
Harry Potter, [and, the chamber, of secrets], Hermione, (Olivanders, Wands)
Returns:
Harry Potter
and, the chamber, of secrets
Hermione
Olivanders, Wands
Run Code Online (Sandbox Code Playgroud)
您可以将以下正则表达式与全局标志一起使用.
,(?![^\(\[]*[\]\)])
Run Code Online (Sandbox Code Playgroud)
这是一个演示.它的灵感来自/sf/answers/632104371/.