title: How to reset entire match at the end of a capturing group?
body: <p>Suppose I have the following text:</p>
  <pre><code>Yes: [x]
  Yes: [  x]
  Yes: [x  ]
  Yes: [  x  ]
  No: [x
  No: x]
  </code></pre>
  <p>I am interested in a <code>regex</code> expression with two capturing groups as follows:</p>
  <ul>
  <li><p>group <code>$1</code>: match the brackets <code>[</code> and <code>]</code> that contain an <code>x</code> in between with a variable amount of horizontal space around <code>x</code>. I can use <a href="https://stackoverflow.com/users/5424988/the-fourth-bird">The fourth bird</a>'s <a href="https://stackoverflow.com/a/70376846/5252007">answer</a> based on a branch reset group for this:</p>
  <ul>
  <li><p><code>regex</code>: <code>(?|(\[)(?=\h*x\h*])|(?&lt;=\[)\h*x\h*(]))</code></p>
  </li>
  <li><p>which results in:</p>
  <p><a href="https://i.stack.imgur.com/IOnnO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IOnnO.png" alt="First capturing group" /></a></p>
  </li>
  </ul>
  </li>
  <li><p>group <code>$2</code>: should match the <code>x</code> that is contained in the brackets <code>[</code> and <code>]</code>. For this, I can use a non-capturing group with a combination of positive lookbehind and lookahead assertions:</p>
  <ul>
  <li><p><code>regex</code>: <code>(?:(?&lt;=\[)\h*(x)(?=\h*]))</code></p>
  </li>
  <li><p>which results in:</p>
  <p><a href="https://i.stack.imgur.com/aSmx6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aSmx6.png" alt="Second capturing group" /></a></p>
  </li>
  </ul>
  </li>
  </ul>
  <p>The problem is that when I try to join the expressions by <code>OR</code>, the second expression does not match anything. For example:</p>
  <pre><code>(?|(\[)(?=\h*x\h*])|(?&lt;=\[)\h*x\h*(]))|(?:(?&lt;=\[)\h*(x)(?=\h*]))
  </code></pre>
  <p>Results in (i.e., see <kbd><a href="https://regex101.com/r/v6zH2m/4" rel="nofollow noreferrer">demo</a></kbd> with <code>extended</code> flag enabled for clarity):</p>
  <p><a href="https://i.stack.imgur.com/Uf2fa.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Uf2fa.jpg" alt="Both capturing groups combined" /></a></p>
  <p>My intuition (i.e., perhaps not correct) is that there is no <code>x</code> left to match for the second expression because the <code>x</code> is matched in the first expression (i.e., group <code>$0</code>). For example, simplifying the second expression to <code>(?:(x))</code> (i.e., see <kbd><a href="https://regex101.com/r/v6zH2m/5" rel="nofollow noreferrer">demo</a></kbd>) will match just fine the <code>x</code> not comprised in brackets as shown below.</p>
  <p><a href="https://i.stack.imgur.com/GmaNK.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GmaNK.jpg" alt="Demonstration of toy example that works" /></a></p>
  <p>Therefore, I thought I should somehow reset the group <code>$0</code> matches from the first expression. So I tried adding the <code>\K</code> meta escape to the first expression before <code>(])</code>, but that did not solve anything.</p>
  <p>Also, as much as possible, I want to stick to the format <code>(?|regex)|(?:regex)|...</code> because I want to be able to extend the expression further with other groups. I am using <a href="https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide#textmate-grammars" rel="nofollow noreferrer">Oniguruma regular expressions</a> and the <a href="https://www.pcre.org" rel="nofollow noreferrer"><code>PCRE</code></a> flavor. Do you have any ideas on how this can be accomplished?</p>
  <p>P.S. Apologies if the title of the question is not entirely accurate.</p>