开发者

Why is (?!^) equivalent to (?<!^)?

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开发者_JAVA技巧
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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜