Prioritize OR Pipe in Regex based on first match in the regex not in the source?
<p>If I do a regex with an <code>OR</code> pipe like <code>(A|B|C)</code></p>

<p><a href="https://regex101.com/r/gB1eP0/4" rel="nofollow">https://regex101.com/r/gB1eP0/4</a></p>

<p>It always finds the first match in order in my source e.g.</p>

<ul>
<li>ABC=A</li>
<li>BCA=B</li>
<li>CBA=C</li>
</ul>

<p>I am trying in fact to set it up so the order I put the options is the order it looks for. In other words in the example I have it would always look for 'A' first and if found stop searching and return <code>A</code>, if not it would search for <code>B</code> and if it didn't find that it would search for <code>C</code> and if found return <code>C</code>. Is there a way to prioritize it that way so in fact in the above example it would find <code>A</code> in each case?</p>

<p>Building on @WiktorStribiÅ¼ew's example:</p>

<p><code>(A|(B|C(?!.*B))(?!.*A))</code></p>

<p>basically, the negative lookahead is used, and then cascaded for all the Or pipes (using the parentheses).</p>

<ol>
<li><p>Match A (A)</p></li>
<li><p>Match B if not followed by A (B)(?!.*A)</p></li>
<li><p>Match C if not followed by B or A (C)(?!.*B)(?!.*A)</p></li>
</ol>

<p><a href="https://regex101.com/r/zQ3pW0/1" rel="nofollow">https://regex101.com/r/zQ3pW0/1</a></p>

python regex