The key concept here is rollback. Whenever a pattern contains quantized subpatterns with different lengths, the regex engine can match strings in different ways, and as soon as the regex part after the quantified part does not match some substring, it can back off, i.e. release char belonging to quantitative pattern and try to combine with subsequent subpatterns.
Look at the larger image:

See how shorter lines match before jumping to longer examples ...
Now, why is not a matching? Because there must be at least 2 characters, since [az]+ and \1+ require a match of at least 1 char.
aa matches because the first ([az]+) first matched the entire line and then went back to place the text for the \1+ pattern (and it matches the second a ), so there is a match.
The three- a line aaa matches in general, because the first ([az]+) first matched the entire line and then went back to place the text for the \1+ template (note that the capture group had to hold only one a , as when trying using two aa , \1+ could not complete the final third a ), and there is a match of three a s.
Now, coming to examples in the question
The string aaaa matches exactly what aa matches: the capture group template first captures all aaaa and then backward, since \1+ must also "find" some text and the regex engine tries to capture aaa in group 1. However, \1+ corresponds to 3 a s, so there is a rollback, and when in group 1 there are two a , then the quantitative backlink coincides with the last two a s.
And the case of the k2 :
The string aaaaa matches as follows:
aaaaa captured and placed in group 1 with part ([az]+)\1+ cannot find any text, the engine retries to match the string in different ways, because the part before \1+ can match another text thanks to the + quantifieraaaa checked (= placed in group 1), to no avail, since \1+ does not match (since then \1 tries to match aaaa , but only a remains to the end of the line)aaa will try again to no avail (since \1 tries to match aaa , but only two a remain)aa is placed in group 1, \1 corresponds to the third and fourth a s, and this is the only match, since only one a remains in the line.
Here is a sample string matching pattern :

Last a cannot be matched:

Wiktor stribiżew
source share