title: How can I force two patterns to capture to the same group in regEx
body: <p>I have to parse a string, formatted as<code>!info1!info2!</code>, and <code>info2</code> is optional.</p>
  
  <p>I am trying to capture, using a regular expression, <code>info1</code> and <code>info2</code> if needed.</p>
  
  <p>I came up with the following pattern:</p>
  
  <pre><code>!([^!]*?)!(.*?)!|!(.*?)! 
  </code></pre>
  
  <p>It works, but I am not satisfied with the results:</p>
  
  <pre><code>!foo!bar! -&gt; Group1: foo Group2:bar
  !foo!     -&gt; Group3: foo
  (https://regex101.com/r/D9d6YP/1)
  </code></pre>
  
  <p>In both expression, <code>foo</code> means the same thing, and is to be process in the same way afterwards. I would like to capture it in the same group, whether or not there is a second group.</p>
  
  <p>I have tried to use named capture groups, but it seems like they can't be reused</p>
  
  <pre><code>!(?&lt;info1&gt;[^!]*?)!(?&lt;info2&gt;.*?)!|!(?&lt;info1&gt;.*?)!
  </code></pre>
  
  <p>fails with the error message 'a sub-pattern name must be unique.</p>
  
  <p>Is there any way to capture different patterns in the same group?</p>