title: using grep linux command with perl regex + capturing groups
body: <p>so I've done some research on the subject and I didn't quite find the perfect solution.
  For example I have a string inside a variable.</p>
  
  <pre><code>var="a1b1c2"
  </code></pre>
  
  <p>now what I want to do is match only "a" follow by any digit, but I only want it to return the number after "a"
  To match it a rule such as</p>
  
  <pre><code>'a\d'
  </code></pre>
  
  <p>and since I only need the digit, I tried with</p>
  
  <pre><code>'a(\d)'
  </code></pre>
  
  <p>and maybe it did capture it somewhere, but I don't know where, the output here is still "a1"</p>
  
  <p>I also tried a non-capturing group to ignore the "a" in the output, but no effect in perl regex:</p>
  
  <pre><code>'(?:a)\d'
  </code></pre>
  
  <p>for reference, this is the full command in my terminal:</p>
  
  <pre><code>[root@host ~]# var="a1b1c2"
  [root@host ~]# echo $var |grep -oP "a(\d)"
  a1 &lt;--output
  </code></pre>
  
  <p>Probably it's also possible without the -P (some not-perl regex format), I'm thankful for every answer :)</p>
  
  <p><strong>EDIT:</strong>
  using</p>
  
  <pre><code>\K
  </code></pre>
  
  <p>is not really the solution, since I don't necessarily need the last part of the match.</p>
  
  <p><strong>EDIT2:</strong>
  I need to able to get any part of the match, for instance:</p>
  
  <pre><code>[root@host ~]# var="a1b1c2"
  [root@host ~]# echo $var |grep -oP "(a)\d"
  a1 &lt;--output
  but the wanted output in this case would be "a"
  </code></pre>
  
  <p><strong>EDIT3:</strong>
  The problem is nearly solved using "look-behind assertions" such as:</p>
  
  <pre><code>(?&lt;=a)\d
  </code></pre>
  
  <p>will not return the letter "a", only the digit following it, but it needs a fixed length, for example it cannot be used as:</p>
  
  <pre><code>(?&lt;=\w+)\d
  </code></pre>
  
  <p><strong>EDIT4:</strong>
  The best way so far is either using perl or combine a combination of look-behind assertions and the \K but it still seems to have some limitations. For example:</p>
  
  <pre><code>1234_foo_1234_bar
  1234567_foo_123456789_bar
  1_foo_12345_bar
  
  if "foo" and "bar" are place-holders for words that don't always have the same length,
  there is no way to match all above examples while output "foobar", since the
  number between them doesn't have a fixed length, while it can't be done with \K since we need "foo"
  </code></pre>
  
  <p>Any further suggestions are still appreciated :)</p>