How can you make the lookahead non-greedy? I would like the first case not to match anything (like the second case), but it returns "winnie". I guess because it is greedily matching after the "the"?
str <- "winnie the pooh bear"
## Unexpected
regmatches(str, gregexpr("winnie|bear(?= bear|pooh)", str, perl=T))
# [1] "winnie"
## Expected
regmatches(str, gregexpr("winnie(?= bear|pooh)", str, perl=T))
# character(0)
The lookahead is being applied to
bear
inwinnie|bear(?= bear|pooh)
and notwinnie
.If you want it to apply on both useNow it will apply on both. Because
winnie
matched theored part bear
never came into picture and neither the lookahead.In the second case
lookahead
is applied onwinnie
.SO it fails.