<p>I would like to convert the following string into an array/nested array: </p>

<pre><code>str = "[[this, is],[a, nested],[array]]"

newarray = # this is what I need help with!

newarray.inspect  # =&gt; [['this','is'],['a','nested'],['array']]
</code></pre>

[{'answer_id': 38477, 'author': 'Ben Childs', 'author_id': 2925, 'author_profile': 'https://Stackoverflow.com/users/2925', 'pm_score': 0, 'selected': False, 'text': "<p>Looks like a basic parsing task. Generally the approach you are going to want to take is to create a recursive function with the following general algorithm</p>\n\n<pre><code>base case (input doesn't begin with '[') return the input\nrecursive case:\n    split the input on ',' (you will need to find commas only at this level)\n    for each sub string call this method again with the sub string\n    return array containing the results from this recursive method\n</code></pre>\n\n<p>The only slighlty tricky part here is splitting the input on a single ','. You could write a separate function for this that would scan through the string and keep a count of the openbrackets - closedbrakets seen so far. Then only split on commas when the count is equal to zero.</p>\n"}, {'answer_id': 38483, 'author': 'user3868', 'author_id': 3868, 'author_profile': 'https://Stackoverflow.com/users/3868', 'pm_score': 0, 'selected': False, 'text': '<p>Make a recursive function that takes the string and an integer offset, and "reads" out an array.  That is, have it return an array or string (that it has read) and an integer offset pointing after the array.  For example:</p>\n\n<pre><code>s = "[[this, is],[a, nested],[array]]"\n\nyourFunc(s, 1) # returns [\'this\', \'is\'] and 11.\nyourFunc(s, 2) # returns \'this\' and 6.\n</code></pre>\n\n<p>Then you can call it with another function that provides an offset of 0, and makes sure that the finishing offset is the length of the string.</p>\n'}, {'answer_id': 38520, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 2, 'selected': False, 'text': '<p>For a laugh:</p>\n\n<pre><code> ary = eval("[[this, is],[a, nested],[array]]".gsub(/(\\w+?)/, "\'\\\\1\'") )\n =&gt; [["this", "is"], ["a", "nested"], ["array"]]\n</code></pre>\n\n<p>Disclaimer: You definitely shouldn\'t do this as <code>eval</code> is a terrible idea, but it is fast and has the useful side effect of throwing an exception if your nested arrays aren\'t valid</p>\n'}, {'answer_id': 40849, 'author': 'Wieczo', 'author_id': 4195, 'author_profile': 'https://Stackoverflow.com/users/4195', 'pm_score': 5, 'selected': True, 'text': '<p>You\'ll get what you want with YAML.</p>\n\n<p>But there is a little problem with your string. YAML expects that there\'s a space behind the comma. So we need this</p>\n\n<pre><code>str = "[[this, is], [a, nested], [array]]"\n</code></pre>\n\n<p>Code:</p>\n\n<pre><code>require \'yaml\'\nstr = "[[this, is],[a, nested],[array]]"\n### transform your string in a valid YAML-String\nstr.gsub!(/(\\,)(\\S)/, "\\\\1 \\\\2")\nYAML::load(str)\n# =&gt; [["this", "is"], ["a", "nested"], ["array"]]\n</code></pre>\n'}, {'answer_id': 89602, 'author': 'glenn mcdonald', 'author_id': 7919, 'author_profile': 'https://Stackoverflow.com/users/7919', 'pm_score': 2, 'selected': False, 'text': '<p>You could also treat it as almost-JSON. If the strings really are only letters, like in your example, then this will work:</p>\n\n<pre><code>JSON.parse(yourarray.gsub(/([a-z]+)/,\'"\\1"\'))\n</code></pre>\n\n<p>If they could have arbitrary characters (other than [ ] , ), you\'d need a little more:</p>\n\n<pre><code>JSON.parse("[[this, is],[a, nested],[array]]".gsub(/, /,",").gsub(/([^\\[\\]\\,]+)/,\'"\\1"\'))\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/38409', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4082/']