The + operator is greedy and inverse as necessary. Looks (?=(\d+)) will match 456, then 56 if the re-expression fails, then 6 if the regex works. First attempt: 456. It matches, group 1 contains 456. Then we have \w+ , which is greedy and accepts 456x56, nothing remains, but we still need to match \1 ie 456. Thus: failure. Then \w+ discards one step at a time until we get to the start of the regular expression. And still fail.
We use a character from a string. The next backtrack tries to find a match with substring 56. it matches, and group 1 contains 56. \w+ matches to the end of the line and gets 456x56, and then we try to match 56: fail. So, \w+ bactracks until we leave 56 in the line, and then we have a global match and a regular expression.
You should try it using debug mode in regex mode.
source share