【发布时间】:2016-05-07 15:54:39
【问题描述】:
我刚刚了解了 Ruby 正则表达式中的 apparently undocumented \K 行为(感谢 this answer by anubhava)。此功能(可能命名为 Keep?)也存在于 PHP、Perl 和 Python 正则表达式中。它在其他地方被描述为“将匹配的内容从要返回的匹配中删除。”
"abc".match(/ab\Kc/) # matches "c"
这种行为是否与下面使用的正向后视标记相同?
"abc".match(/(?<=ab)c/) # matches "c"
如果不是,两者有什么不同?
【问题讨论】:
-
一个区别是,lookbehind 不消耗字符。请参阅此示例 with lookbehind 与此 with use of \K。
标签: php python ruby regex perl