DEV Community

Maroun Maroun
Maroun Maroun

Posted on

1 2

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:

a\Kb

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)'
world
~$ echo 'hello world' | grep -oP 'hello (world)'
hello world

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay