53

The \K escape sequence resets the beginning of the match to the current position in the token list (this only affects what is reported as the full match).

What environments/languages/versions support \K (keep) in its regular expression engines and what libraries are needed (if any) to use this feature within patterns?

Toto
  • 86,179
  • 61
  • 85
  • 118
Ωmega
  • 40,237
  • 31
  • 123
  • 190
  • 2
    To aid searches, `\K` is also known as a regular expression [metacharacter](http://en.wikipedia.org/wiki/Metacharacter). – DavidRR Feb 28 '14 at 13:55

1 Answers1

53

The \K escape sequence is supported by several engines, languages or tools, such as:

...and (so far) not supported by:

  • .NET
  • awk
  • bash
  • ICU
  • Java
  • Javascript
  • Objective-C
  • POSIX
  • Python
  • Qt/QRegExp
  • sed
  • Tcl
  • vim --------------------⇢ It doesn't have \K, but its \zs is equivalent
  • XML
  • XPath
Mahozad
  • 11,316
  • 11
  • 73
  • 98
Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206
  • AutoIt does too. Not sure how long it has support it for though. – James Aug 07 '13 at 16:43
  • 2
    This answer has been added to the [Stack Overflow Regular Expression FAQ](http://stackoverflow.com/a/22944075/2736496), under "Lookarounds". – aliteralmind Apr 10 '14 at 00:32
  • \k named backreferences are supported in .NET: https://docs.microsoft.com/en-us/dotnet/standard/base-types/backreference-constructs-in-regular-expressions#named-backreferences – juFo Jun 25 '18 at 07:55
  • 1
    @juFo I think the question is about uppercase `\K` which is different from lowercase `\k` in regex. – Mahozad May 09 '22 at 17:26