55

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
  • 89,455
  • 62
  • 89
  • 125
Ωmega
  • 42,614
  • 34
  • 134
  • 203
  • 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

54

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
  • 18,032
  • 13
  • 118
  • 133
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • 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://learn.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