2016-08-03 171 views
1

我有一個很長的字符串,我想通過用逗號分割它,只要逗號不在方括號或括號內就可以將它設置爲一個數組。我試過一對夫婦的變化,但正是我要找的......用逗號分割字符串(如果不在方括號內或括號內)

例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

@Marc - 我看到了這個問題,但我仍然不清楚如何解釋我正在尋找的兩個條件。 – Keren

+0

由於這個問題是關於括號**和**括號重新打開的:完整答案更難。 – Bathsheba

+1

@Bathsheba http://stackoverflow.com/questions/28587081/regex-split-on-comma-but-exclude-commas-within-parentheses-and-quotesboth-s –

回答

相關問題