title: Regex for replacing commas only within square brackets
body: <p>I have a text file and it contains comma separated strings.  But some of the strings separated by commas are of the form <code>[*,*,*,...]</code>.  So for example:</p>
  
  <p>"Hello", "Goodbye", ["Yes", "No", "Maybe], "Indeed", ["Why", "What"]</p>
  
  <p>I want to be able to parse the file to replace only commas within square brackets with a semicolon.  There can be any number of brackets and any number of commas within the brackets.</p>
  
  <p>I tried using this code in R but its not working as planned, some commas outside my brackets are being replaced:</p>
  
  <pre class="lang-r prettyprint-override"><code>repeat{
            tmp &lt;- gsub("(\\[.*\\K),(?=.*\\])", ";", tmp, perl = TRUE) # replace last comma found within braces with semicolon
            if (sum(grepl("(\\[.*\\K),(?=.*\\])", tmp, perl = TRUE)) == 0) {  # repeat until no more commas found
              break
            }
          }
  </code></pre>
  
  <p>Can anyone help with regex that can solve this problem?  Thanks!</p>