Optimize Or Pipes (|) in Regex
<p>I am running a multi-pipe Regex on text blocks that are about 3,000 characters long. I have 6 different and the matches are always in the same order relative to one another and complicating it is I always want to prioritize the last one firsst</p>

<pre><code>Pattern1|Pattern2|Pattern3|Pattern4|Pattern5|Pattern6
</code></pre>

<p>Right now I am testing on a block of text that finds Pattern1. Standalone it takes 41 steps, in the pipes it takes over 30,000. I get that it might take more specific information for this specific problem, but was wondering if there were some generic steps to take to make and/or pipes more efficient. Is there an "order" that helps? Clearly this is not testing the first pattern and then quitting since it would still be 41 steps so wondering if I need to adhere to some basic and/or construction I am unaware of.</p>

<p>The main point about optimizing alternation groups is <strong>the alternative branches should not match at the same location</strong>.</p>

<p>Consider a string that has many similar substrings to those in your pattern above:</p>

<pre><code>Pattern  Pattern Pattern Pattern Pattern Pattern  Pattern Pattern Pattern Pattern Pattern  Pattern Pattern Pattern Pattern Pattern  Pattern Pattern Pattern Pattern Pattern  Pattern Pattern Pattern Pattern Pattern  Pattern Pattern Pattern Pattern Pattern  Pattern Pattern Pattern Pattern Pattern  Pattern Pattern Pattern Pattern Pattern2
</code></pre>

<p>See the <a href="https://regex101.com/r/iU6nN2/1" rel="nofollow">regex demo</a>. The <code>Pattern1</code> is tried first, and it matches <code>Pattern</code> but since there is no digit after it, the alternative is discarded and the next branch is tried. <code>Pattern2</code> also goes up to the digit, but there is no digit. And so on. </p>

<p>If you make your regex start with a common prefix and then use a group for the different endings - like <a href="https://regex101.com/r/iU6nN2/2" rel="nofollow"><code>Pattern(?:1|2|3|4|5|6)</code></a> - a lot of redundant backtracking is spared. </p>

python regex