I have the following string: "text before AB000CD000CD text after"
. I want to match text from AB to the first occurrence of CD. Inspired by this answer, I created the following regex pattern:
AB((?!CD).)*CD
I checked the result in https://regex101.com/ and the output is:
Full match 12-19 `AB000CD`
Group 1. 16-17 `0`
Looks like it does what I need. However I don't understand why it works. My understanding is that my pattern should match AB first, then any character that is not followed by CD, and then CD itself. But following this logic, the result should not include 000, but only 00 because the last zero is actually followed by CD. Is my explanation wrong?