title: How to capture a group in a sed that uses bash variables
body: <p>I am using sed with variables in a bash script to parse markdown images <code>![Legend](path/image.png)</code>, and I am trying to capture a group of caracters in the same time.</p>
  
  <p>The syntax for a "normal" regex would be like this to capture 1. the image legend, 2. the image path : <code>!\[(.*)\]\((.*)\)</code></p>
  
  <p>For instance from <code>![[Muybridge, Eadward](https://www.britannica.com/biography/Eadweard-Muybridge/images-videos)(1887)](https://i.imgur.com/p2pwfme.jpg)</code> would gives :</p>
  
  <ol>
  <li>legend : <code>[Muybridge, Eadward](https://www.britannica.com/biography/Eadweard-Muybridge/images-videos)(1887)</code></li>
  <li>path : <code>https://i.imgur.com/p2pwfme.jpg</code></li>
  </ol>
  
  <p>But the normal regex complexifies within the sed using a variable. </p>
  
  <p>Here is the piece of the scripts that parses the images in a markdown document (my question addresses the last sed command):</p>
  
  <pre class="lang-sh prettyprint-override"><code>for filename in "$3"*; do
    COUNTER=$[$COUNTER +1]
    echo "&lt;b&gt;${filename%.*}&lt;/b&gt; will be renamed &lt;b&gt;${GET[id]}-img${COUNTER}.*&lt;/b&gt;&lt;br&gt;"
    sed -i -e "s@${filename%.*}@${GET[id]}\-img${COUNTER}@g" ../${GET[id]}.md
    mv ${filename%.*}.${filename##*.} ${GET[id]}-img${COUNTER}.${filename##*.}
    sed -i -e "s@!['('.*')'](.*${GET[id]}\-img${COUNTER}@![\1](media\/${GET[id]}\-img${COUNTER}@g" ../${GET[id]}.md
  done
  </code></pre>
  
  <p>You will see that:</p>
  
  <ol>
  <li>I use double quotes since sed calls variables</li>
  <li>I use <code>@</code> seperator instead of <code>/</code></li>
  <li>I don't need to escape the <code>[</code> <code>]</code> in this context (this is confirmed)</li>
  <li>I tried to escape the group capturing parentheses with <code>'('.*')'</code> but it doesn't work, also tried with <code>\(.*\)</code></li>
  </ol>
  
  <p>I am therefore wondering how to capture a group (which is how to correctly use <code>()</code> according to regex syntax) in such context : bash+sed+variables.</p>