title: regex to replace text *outside* of {}
body: <p>I want to use regex to replace commands or tags around strings. My use case is converting LaTeX commands to bookdown commands, which means doing things like replacing <code>\citep{*}</code> with <code>[@*]</code>, <code>\ref{*}</code> with <code>\@ref(*)</code>, etc. However, lets stick to the generalized question:</p>
  
  <p>Given a string <code>&lt;begin&gt;somestring&lt;end&gt;</code> where <code>&lt;begin&gt;</code> and <code>&lt;end&gt;</code> are known  and <code>somestring</code> is an arbitrary sequence of characters, can we use regex to susbstitute <code>&lt;newbegin&gt;</code> and <code>&lt;newend&gt;</code> to get the string <code>&lt;newbegin&gt;somestring&lt;newend&gt;</code>?</p>
  
  <p>For example, consider the LaTeX command <code>\citep{bonobo2017}</code>, which I want to convert to <code>[@bonobo2017]</code>. For this example:</p>
  
  <pre><code>&lt;begin&gt; = \citep{
  somestring = bonobo2017
  &lt;end&gt; = }
  &lt;newbegin&gt; = [@
  &lt;newend&gt; = ]
  </code></pre>
  
  <p>This question is basically the inverse of <a href="https://stackoverflow.com/questions/18397720/java-regex-to-replace-text-between-dollar-signs">this question</a>.</p>
  
  <p>I'm hoping for an R or notepad++ solution.</p>
  
  <p><strong>Additional Examples</strong></p>
  
  <ol>
  <li>Convert <code>\citet{bonobo2017}</code> to <code>@bonobo2017</code></li>
  <li>Convert <code>\ref{myfigure}</code> to <code>\@ref(myfigure)</code></li>
  <li>Convert <code>\section{Some title}</code> to <code># Some title</code></li>
  <li>Convert <code>\emph{something important}</code> to <code>*something important*</code></li>
  </ol>
  
  <p>I'm looking for a template regex that I can fill in my <code>&lt;begin&gt;</code>, <code>&lt;end&gt;</code>, <code>&lt;newbegin&gt;</code> and <code>&lt;newend&gt;</code> on a case-by-case basis.</p>