Why (?! ^) Equivalent to (? <! ^)?

Some time ago I answered this question on SO, but I still do not understand why my answer worked.

For some reason, a negative result for the beginning of a line 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 that this is not the most useful question that has ever been asked, but it has already imposed on me a couple of weeks.

+5
source share
1 answer

A carriage is a zero width statement. In fact, looks and lookbehind also have zero width. Therefore, in this case it does not matter whether you look forward or backward, you are still looking at the same position of the symbol.

.

+8

All Articles