Your assumption that the regex matches a longer interlace is incorrect.
If you have some time, let's see how your regular expression works ...
Quick update: how the regular expression works: the state machine always reads from left to right, returning to where it is needed.
There are two pointers, one to the template:
(cdefghijkl|bcd)
Another in your line:
abcdefghijklmnopqrstuvw
The pointer to the line moves to the left. As soon as he can return, he will :

(source: gyazo.com )
Let me turn this into a more "consistent" sequence for understanding:

(source: gyazo.com )
Your foobar example is another topic. As I mentioned in this post :
How a regular expression works: a state machine always reads from left to right. ,|,, == , since it will always correspond only to the first rotation.
That's fine, Uniedr, but how do I get him to the first rotation?
Look! [TG44]
^(?:.*?\Kcdefghijkl|.*?\Kbcd)
There is a demonstration of regular expressions here .
This regular expression first tries to match the entire string with the first interlace. Only if he fails completely will he try to match the second rotation. \K used here to maintain consistency with the contents behind the \K construct.
* : \K supported in Ruby since 2.0.0.
Read more:
Ah, I was bored, so I optimized the regex:
^(?:(?:(?!cdefghijkl)c?[^c]*)++\Kcdefghijkl|(?:(?!bcd)b?[^b]*)++\Kbcd)
You can see the demo here .