title: bash - print regex captured groups
body: <p>I have a <strong>file.xml</strong> so composed:</p>
  <pre><code>...some xml text here...
      &lt;Version&gt;1.0.13-alpha&lt;/Version&gt;
  ...some xml text here...
  </code></pre>
  <p>I need to extract the following information:</p>
  <ul>
  <li><em>mayor_and_minor_release_number</em> --&gt; 1.0</li>
  <li><em>patch_number</em> --&gt; 13</li>
  <li><em>suffix</em> --&gt; -alpha</li>
  </ul>
  <p>I've thought the cleanest way to achieve that is by mean of a regex with <strong>grep</strong> command:</p>
  <pre><code>&lt;Version&gt;(\d+\.\d+)\.(\d+)([\w-]+)?&lt;\/Version&gt;
  </code></pre>
  <p>I've checked with <a href="https://regex101.com/" rel="nofollow noreferrer">regex101</a> the correctness of this regex and actually it seems to properly capture the 3 fields I'm looking for. But here comes the problem, since I have no idea how to print those fields.</p>
  <pre><code>cat file.xml | grep &quot;&lt;Version&gt;(\d+\.\d+)\.(\d+)([\w-]+)?&lt;\/Version&gt;&quot; -oP
  </code></pre>
  <p>This command prints the entire line so it's quite useless.</p>
  <p>Several posts on this site have been written about this topic, so I've also tried to use the <em>bash native
  regex support</em>, with poor results:</p>
  <pre><code>regex=&quot;&lt;Version&gt;(\d+\.\d+)\.(\d+)([\w-]+)?&lt;\/Version&gt;&quot;
  txt=$(cat file.xml)
  [[ &quot;$txt&quot; =~ $regex ]]     --&gt; it fails!
  echo &quot;${BASH_REMATCH[*]}&quot;
  </code></pre>
  <p>I'm sorry but I cannot figure out how to overtake this issue. The desired output should be:</p>
  <pre><code>1.0
  13
  -alpha
  </code></pre>