48

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
  • 83,193
  • 59
  • 77
  • 109
Ωmega
  • 37,727
  • 29
  • 115
  • 183
  • 1
    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

51

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

  • boost (since ???)
  • grep -P                                                     ← uses PCRE
  • Oniguruma (since 5.13.3)
  • PCRE (since 7.2)
  • Perl (since 5.10.0)
  • PHP (since 5.2.4)
  • Ruby (since 2.0.0)
  • Notepad++ (since 6.0)

...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
Gilles Quenot
  • 143,367
  • 32
  • 199
  • 195
  • 2
    Update: It is supported in Ruby as of 2.0.0 due to its use of the Onigmo engine (a fork of Oniguruma). – Peter Cooper Feb 09 '13 at 21:24
  • 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
  • 4
    Supported in *Notepad++* since [version 6.0](http://notepad-plus-plus.org/news/notepad-6.0-release.html) ? – Jonny 5 Aug 31 '14 at 14:10
  • 1
    Hummmm, what is GNU ??? I don't thing GNU as a tool but a full set of tools – Gilles Quenot Feb 17 '17 at 21:16
  • \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
  • vim's equivalent of `\K` is `\zs`. It also has `\ze` to mark the end of a match. – JoL Aug 19 '19 at 20:04