title: How do I create a dynamic capturing group in regex?
body: <p>I have a string like this:</p>
  
  <pre><code>$str ="
  - group1
  - group2
  - group3
  ";
  </code></pre>
  
  <p>Also I have this <a href="https://regex101.com/r/uE6jQ1/3" rel="noreferrer">regex</a>:</p>
  
  <pre><code>/(\-\s\w+)\n(\-\s\w+)\n(\-\s\w+)/
  </code></pre>
  
  <p>As you know, there is three capturing groups: <code>$1</code>, <code>$2</code> and <code>$3</code>. I made those group manually. I mean if I append this to the string above:</p>
  
  <pre><code>- group4 
  - group5
  </code></pre>
  
  <p>Then that regex doesn't matches them.</p>
  
  <hr>
  
  <p>Well, I have a constant pattern: <code>(\-\s\w+)</code>, And I want to create a separated capturing group on the number of matches items in the string. Hare is a few examples:</p>
  
  <p><strong>Example1:</strong></p>
  
  <pre><code>$str="
  - group 1
  ";
  </code></pre>
  
  <p>I need a regex to give me all the string by <code>$1</code>.</p>
  
  <hr>
  
  <p><strong>Example2:</strong></p>
  
  <pre><code>$str="
  - group 1
  - group 2
  ";
  </code></pre>
  
  <p>I need a regex to give me first line of string (<code>- group 1</code>) by <code>$1</code> and second line (<code>- group 2</code>) by <code>$2</code> </p>
  
  <hr>
  
  <p>Ok well, as you see in the examples above, string is dynamic but it is according to a constant pattern ... Now I want to know how can I create a dynamic capturing group according to the string?</p>