Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Firstly, <code>\w</code> is not a letter, but <code>[a-zA-Z0-9_]</code>. So if you really just want letters there make that <code>[a-zA-Z]</code>.</p> <p>Secondly, I suppose you might have a case of <a href="http://www.regular-expressions.info/catastrophic.html">catastrophic backtracking</a>.</p> <p>Your regex obviously does not go past <code>OXM</code>, because there is no way to match three consecutive letters in your pattern. And if you remove the <code>$</code> anchor, the regex will gladly match there, but when you leave it then the regex will fail and start to backtrack.</p> <p>So suppose it matched the <code>OX</code> with <code>\w{1,2}</code> and failed. Then it will throw away the last repetition of the whole second non-capturing group and go back one step, where it matched <code>7</code> with with <code>[-\s\d]</code>. Now it will try to match <code>7O</code> or <code>7</code> with <code>\w{1,2}</code> instead, but then again fails to match <code>[-\d\s]</code> against <code>X</code> or <code>O</code>, respectively. Another step back, it tries to rematch <code>27</code> or <code>2</code> with <code>\w{1,2}</code> and fails again. And so on and so on. The further you go back, it might be possible again to match the <code>[-\d\s]</code> against a letter, and then the engine will go all the way forward to <code>OXM</code> again and start the fun again. When backtracking finally reaches the beginning of the string and your very first alternation, it will try all three options for that alternation, too, and will do the whole thing over and over again.</p> <p>Let me try to visualize the first steps of the backtracking by writing out which alternations are used in the repetition. The first of each two lines is the tested string, the second contains the corresponding regex constructs used. Each attempt fails at the last character.</p> <pre><code>... 1 2 7 O ... [-\s\d] [-\s\d] [-\s\d] [-\s\d] ... 1 2 7 OX M ... [-\s\d] [-\s\d] [-\s\d] \w{2} [-\d\s] ... 1 2 7 O X ... [-\s\d] [-\s\d] [-\s\d] \w{1} [-\d\s] ... 1 2 7O X ... [-\s\d] [-\s\d] \w{2} [-\d\s] ... 1 2 7 O ... [-\s\d] [-\s\d] \w{1} [-\d\s] ... 1 27 O ... [-\s\d] \w{2} [-\d\s] ... 1 2 7 O ... [-\s\d] \w{1} [-\d\s] [-\s\d] ... 1 2 7 OX M ... [-\s\d] \w{1} [-\s\d] \w{2} [-\d\s] ... 1 2 7O X ... [-\s\d] \w{1} \w{2} [-\d\s] </code></pre> <p>And so on. I hope you get the idea. It's hard to visualize it in a few lines of ASCII.</p> <p>I suppose, just changing <code>\w</code> to the appropriate character group might already solve the problem, because there are less equivalent combinations. Try it out.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload