DEV Community

Maroun Maroun
Maroun Maroun

Posted on

What Does "\K" Mean in Regex?

Since not all regex flavors support lookarounds, Perl introduced the match reset - \K (bolded is mine):

\K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match

To make the explanation short, consider the following simple Regex:


When "b" is matched, \K tells the Regex engine to pretend that the match attempt started at this position.

~$ echo 'hello world' | grep -oP 'hello \K(world)'
~$ echo 'hello world' | grep -oP 'hello (world)'
hello world

Top comments (0)