I just learned about the apparently undocumented
\K behavior in Ruby regex (thanks to this answer by anubhava). This feature (possibly named Keep?) also exists in PHP, Perl, and Python regex flavors. It is described elsewhere as “drops what was matched so far from the match to be returned.”
"abc".match(/ab\Kc/) # matches "c"
Is this behavior identical to the positive lookbehind marker as used below?
"abc".match(/(?<=ab)c/) # matches "c"
If not, what differences do the two exhibit?
It’s easier to see the difference between
(?<=...) with the
A lookbehind is a zero-width assertion that doesn’t consume characters and that is tested (backwards) from the current position:
> "abcdefg".scan(/(?<=.)./) => ["b", "c", "d", "e", "f", "g"]
The “keep” feature
\K (that isn’t an anchor) defines a position in the pattern where all that was matched so far by the pattern on the left is removed from the match result. But all characters matched before the
\K are consumed, they just don’t appear in the result:
> "abcdefg".scan(/.\K./) => ["b", "d", "f"]
The behaviour is the same as without
> "abcdefg".scan(/../) => ["ab", "cd", "ef"]
except that the characters before the
\K are removed from the result.
One interesting use of
\K is to emulate a variable-length lookbehind, which is not allowed in Ruby (the same for PHP and Perl), or to avoid the creation of a unique capture group. For example
(?<=a.*)f. can be implemented using
> "abcdefg".match(/a.*\Kf./) => #<MatchData "fg">
An alternative way would be to write
/a.*(f.)/, but the
\K avoids the need to create a capture group.
Note that the
\K feature also exists in the python regex module, even this one allows variable-length lookbehinds.