In (?=abc)def capture (?=abc) is zero width and does not move the cursor forward in the input line after a successful match. This construct simply says, looking ahead at the next three characters to see if they are abc if they then check to see if the same characters are def . At this point, the match fails.
You need to understand how the regex engine works to complete the match. Consider your abcdef input line and your regular expression abc(?=def) . The engine starts by matching with a , then moves the cursor inside the input line to the next character and tries to match b , because the cursor in the input line is at b , the match succeeds. Then the engine moves the cursor inside the input line and tries to combine c , and since the cursor is in the input line by c , the match is successful, and the cursor in the input line moves again to the next character. Now the engine encounters (?=def) , at that moment the engine simply looks ahead to see if the next three characters, of which the cursor is in the input sting, are actually def without moving the cursor that they are, and the match succeeds.
Now consider the input string xyz and the regular expression x(?=y)Z The regex engine puts the cursor in the first letter of the input line and checks if it is x and finds that x , so it moves the cursor to the next character in the input line. Now he expects whether the next character is y , as it is, but the engine does not move the entered text cursor preface, so the cursor in the input text remains at y . The engine then looks to see if the cursor is on the letter z , but since the cursor in the input text is still on the letter y , no match is made.
You can read a lot more about positive and negative images at http://www.regular-expressions.info/lookaround.html
source share