A little while back I answered this question on SO, but I still haven't figured out why my answer worked.
For some reason, a negative lookahead for the start of a string behaves the same as a negative lookbehind.
For example, in PHP
preg_replace("/(?!^)12/", "ab", "12345"); // 12345
preg_replace("/(?<!^)12/", "ab", "12345"); // 12345
preg_replace("/(?!1)23/", "ab", "12345"); // 1ab45
preg_replace("/(?<!1)23/", "ab", "12345"); // 12345
I know it's not the most useful question ever asked, but this has been nagging me for a couple of weeks.
The caret is a zero-width assertion. In fact lookaheads and lookbehinds are zero-width as well. Therefore, in this case it doesn't matter if you're looking ahead or behind, you're still looking at the same character position.
This is explained pretty well in this article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With