in reply to Practical use cases for \K in regex

G'day perlboy_emeritus,

Use case: You have error and warning exceptions containing variable-length codes. You want to embed a timestamp before sending to a logfile.

$ perl -E ' my @exceptions = ( "ERROR (EABC): Error message 1", "WARNING (WPQRST): Error message 2", "ERROR (EWXYZ): Error message 3", ); my $re = qr{^(?:ERR|WAR)[^:]+: \K.+$}; for my $e (@exceptions) { my $timestamp = localtime; $e =~ s/$re/[$timestamp] $&/; say $e; sleep 2; } ' ERROR (EABC): [Fri Oct 6 02:56:17 2023] Error message 1 WARNING (WPQRST): [Fri Oct 6 02:56:19 2023] Error message 2 ERROR (EWXYZ): [Fri Oct 6 02:56:21 2023] Error message 3

I'd suggest doing a Super Search for \K to get a wide range of examples.

Hints when searching: Ignore posts by me (kcott) as many with MSWin examples contain paths with "...\Ken\...". Monks whom I've noticed over the years using \K often, in many scenarios, are tybalt89 and AnomalousMonk. I'm not suggesting that you limit your search but those three usernames featured many times in my search results for just \K.

— Ken