Practical Regex 4: Use \K as an Alternative to Positive Lookbehind
Say you have this text:
Order ID: INV-2026-001
You want to match only INV-2026-001, but only when it is preceded by Order ID:. The classic approach is to use positive lookbehind:
(?<=Order ID: )INV-\d{4}-\d+
This works fine. The lookbehind asserts that Order ID: must exist before the match, but it is not included in the result.
However, in PCRE, there is another practical alternative: \K.
\K tells the regex engine to reset the start of the current match. In simpler words, think of it as a marker that says:
My real match starts here.
Here is the same pattern rewritten with \K:
Order ID: \KINV-\d{4}-\d+
Instead of asserting what came before, you match the prefix normally and then keep it out of the final match with \K.
So the regex still requires “Order ID: “, but the returned match is only INV-2026-001.
Let’s use real code to get it, for example, with PHP:
$text = <<<TEXT
My Order ID: INV-2026-001
TEXT;
$pattern = '#Order ID: \KINV-\d{4}-\d+#';
preg_match($pattern, $text, $matches);
if ($matches !== []) {
var_dump($matches[0]);
}
The output is:
string(12) "INV-2026-001"
Ref https://3v4l.org/IakBf#v8.5.3
Beware:
- ⚠️ The
\Kis not supported by every regex engine. It works in PCRE, which is used by PHP’spreg_*functions, but do not assume it works in other languages. For example, it doesn’t work in JavaScript. -
⚠️ The
\Kalso has limitation, you can read at https://www.regular-expressions.info/keep.html
Final words:
\K is practical for extraction when the left side is only context. However, it is not exactly the same as positive lookbehind. It resets the returned match, not the matching process. For overlapping matches, use real positive lookbehind.
That’s it 😉
leave a comment