2016-08-03 4 views
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 –

答えて

3

私は...

例1のバリエーションのカップルを試みたが、私が探している正確に何を取得していませんでしたグローバルフラグを持つ次の正規表現。

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

ここにはdemoがあります。 それはhttps://stackoverflow.com/a/9030062/1630604に触発されています。

関連する問題