Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

In regex, what's the difference between a lookbehind and \K? [duplicate]

$
0
0

This question already has an answer here:

I just learned about \K in regex, and was wondering if somebody could explain how it's different from a lookbehind.

For example, the following two gsubs are identical, as far as I can tell:

tst<-"This is the day. That is the day"
gsub("This is the \\Kday","week",tst, perl=TRUE)
gsub("(?<=This is the )day","week",tst,perl=TRUE)

Maybe I'm not thinking complex enough, but can anyone give an example where one makes sense but the other doesn't and vice versa?

One thing I could think of is that a lookbehind has to be of a fixed length (right? I get an error when I try to use .* in a lookbehind), but then why use a lookbehind instead of \K?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>