Split string based on number of commas
<p>I have a text which is splited with commas.</p>

<p>e.g.:</p>

<pre><code>FOO( something, BOO(tmp, temp), something else)
</code></pre>

<p>It could be that <em>something else</em> contain as well a string with commas...</p>

<p>I would like to split the text inside the brakets of FOO to its elements and then pasrse the elements.</p>

<p>What i do know is that <em>FOO</em> must have two commas.</p>

<p>How could I split the contant of <em>FOO</em> to its three elements?</p>

<p><strong>Remark:</strong> <em>something else</em> could be <em>BOO(ddd, ddd)</em> or simply <em>ddd</em>. I can not assume a simple regex regel of <em>'FOO\(\w+, BOO(\w+, \w+), \w+\)'</em></p>

<p>Assuming that the string is Python code you can use <strong>parser</strong> for this. If you look carefully at the result you might agree that it's not as bad as it first appears to be.</p>

<pre><code>&gt;&gt;&gt; from parser import *
&gt;&gt;&gt; source="FOO( something, BOO(tmp, temp), something)"
&gt;&gt;&gt; st=suite(source)
&gt;&gt;&gt; st2tuple(st)
(257, (268, (269, (270, (271, (272, (302, (306, (307, (308, (309, (312, (313, (314, (315, (316, (317, (318, (319, (320, (1, 'FOO')), (322, (7, '('), (330, (331, (302, (306, (307, (308, (309, (312, (313, (314, (315, (316, (317, (318, (319, (320, (1, 'something')))))))))))))))), (12, ','), (331, (302, (306, (307, (308, (309, (312, (313, (314, (315, (316, (317, (318, (319, (320, (1, 'BOO')), (322, (7, '('), (330, (331, (302, (306, (307, (308, (309, (312, (313, (314, (315, (316, (317, (318, (319, (320, (1, 'tmp')))))))))))))))), (12, ','), (331, (302, (306, (307, (308, (309, (312, (313, (314, (315, (316, (317, (318, (319, (320, (1, 'temp'))))))))))))))))), (8, ')')))))))))))))))), (12, ','), (331, (302, (306, (307, (308, (309, (312, (313, (314, (315, (316, (317, (318, (319, (320, (1, 'something'))))))))))))))))), (8, ')')))))))))))))))))), (4, ''))), (4, ''), (0, ''))
</code></pre>

python regex