如果不在方括号或括号内,请用逗号分隔字符串 [英] Split string by comma if not within square brackets or parentheses
本文介绍了如果不在方括号或括号内,请用逗号分隔字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个长字符串,只要逗号不在方括号或括号内,我想通过用逗号分隔将其设置为数组.我尝试了几种变体,但未完全得到我想要的...
I have a long string, and I want to set it to an array by splitting it by the comma as long as the comma is not within square brackets or parentheses. I've tried a couple of variations but not getting exactly what I'm looking for...
示例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
推荐答案
您可以将以下正则表达式与全局标志一起使用.
You can use the following regex with global flag.
,(?![^\(\[]*[\]\)])
这是演示.它受到 https://stackoverflow.com/a/9030062/1630604 的启发.
这篇关于如果不在方括号或括号内,请用逗号分隔字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文