The first pattern:
re.search('(\d*)(?<=a)(\.)', '1a.')
says to find zero or more digits, followed by a dot. Right before the dot, it has a positive lookbehind, which asserts the previous character was an a
. In this case, Python will match zero digits, followed by a single dot. The lookbehind fires true, because the preceding character was in fact an a
.
However, the second pattern:
re.search('(\d+)(?<=a)(\.)','1a.')
matches one or more digits, followed the lookbehind and matching dot. In this case, Python is compelled to match the number 1
. But then it the lookbehind must fail. Obviously, if the last character matched were a number, it cannot be the letter a
. So, there is no match possible in the second case. Even if we were to remove (?<=a)
from the second pattern, it would still fail because we are not accounting for the letter a
.