title: Vim / sed regex backreference in search pattern
body: <p>Vim help says that:</p>
  
  <blockquote>
  <pre><code>\1      Matches the same string that was matched by     */\1* *E65*
          the first sub-expression in \( and \). {not in Vi}
          Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc. 
  </code></pre>
  </blockquote>
  
  <p>It looks like the <em>backreference</em> can be used in search pattern. I started playing with it and I noticed behavior that I can't explain. This is my file:</p>
  
  <pre><code>&lt;paper-input label="Input label"&gt; Some text &lt;/paper-input&gt;
  &lt;paper-input label="Input label"&gt; Some text &lt;/paper-inputa&gt;
  &lt;aza&gt; Some text &lt;/az&gt;
  &lt;az&gt; Some text &lt;/az&gt;
  &lt;az&gt; Some text &lt;/aza&gt;
  </code></pre>
  
  <p>I wanted to match the lines where the opening and closing tags are matching i.e.:</p>
  
  <pre><code>&lt;paper-input label="Input label"&gt; Some text &lt;/paper-input&gt;
  &lt;az&gt; Some text &lt;/az&gt;
  </code></pre>
  
  <p>And my test regex is:</p>
  
  <pre><code>%s,&lt;\([^ &gt;]\+\).*&lt;\/\1&gt;,,gn
  </code></pre>
  
  <p>But this matches lines: <code>1</code>, <code>3</code> and <code>4</code>. Same thing with <strong>sed</strong>:</p>
  
  <pre><code>$ sed -ne 's,&lt;\([^ &gt;]\+\).*&lt;\/\1&gt;,\0,p' file
  &lt;paper-input label="Input label"&gt; Some text &lt;/paper-input&gt;
  &lt;aza&gt; Some text &lt;/az&gt;
  &lt;az&gt; Some text &lt;/az&gt;
  </code></pre>
  
  <p>This: <code>&lt;\([^ &gt;]\+\)</code> should be greedy and when trying to match it without <code>\1</code> at the end then all the groups are correct. But when I add <code>\1</code> it seems that <code>&lt;\([^ &gt;]\+\)</code> becomes not greedy and it tries to force the match in <em>3rd line</em>. Can someone explain why it matches <code>3rd</code> line:</p>
  
  <pre><code>&lt;aza&gt; Some text &lt;/az&gt;
  </code></pre>
  
  <p>This is also a <a href="https://regex101.com/r/nT4fP6/1" rel="nofollow noreferrer">regex101 demo</a></p>
  
  <p><strong>NOTE</strong>
  This is not about the regex itself (probably there is other way to do it) but about the behavior of that regex.</p>