title: Replace string between two patterns with sed - no error, no result
body: <p>Example from the file where I want to change the username:</p>
  
  <pre><code>&lt;host&gt;&lt;![CDATA[localhost]]&gt;&lt;/host&gt;
  &lt;username&gt;&lt;![CDATA[root]]&gt;&lt;/username&gt;
  </code></pre>
  
  <p><a href="https://regex101.com/r/MyqtaQ/1" rel="nofollow noreferrer">Here is my regex test</a> to ensure it matches.</p>
  
  <p>And finally the sed command that completes without error and does not change anything:</p>
  
  <pre><code>sed -i -e 's/(&lt;username&gt;&lt;!\[CDATA\[).*(\]\]&gt;&lt;\/username&gt;)/TEST/g' path/to/file.xml
  </code></pre>
  
  <p>This should be the result:</p>
  
  <pre><code>&lt;host&gt;&lt;![CDATA[localhost]]&gt;&lt;/host&gt;
  TEST
  </code></pre>
  
  <hr>
  
  <p>Besides it not replacing at all, I need it to work like this in order to keep the capturing groups before and after, effectively only replacing the username:</p>
  
  <pre><code>sed -i -e 's/(&lt;username&gt;&lt;!\[CDATA\[).*(\]\]&gt;&lt;\/username&gt;)/\1TEST\2/g' path/to/file.xml
  </code></pre>
  
  <p>But that returns this error:</p>
  
  <pre><code>sed: -e expression #1, char 57: invalid reference \2 on `s' command's RHS
  </code></pre>
  
  <p>So I think the capturing groups are matched to loosely somehow since it doesn't really capture them, which makes the replace not replace anything in the first place?</p>
  
  <p>The end result I want is this:</p>
  
  <pre><code>&lt;host&gt;&lt;![CDATA[localhost]]&gt;&lt;/host&gt;
  &lt;username&gt;&lt;![CDATA[TEST]]&gt;&lt;/username&gt;
  </code></pre>