Lookahead Behaviour

103 views Asked by At

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)
1

There are 1 answers

1
vks On BEST ANSWER

The lookahead is being applied to bear in winnie|bear(?= bear|pooh) and not winnie.If you want it to apply on both use

(?:winnie|bear)(?= bear|pooh)

Now it will apply on both. Because winnie matched the ored part bear never came into picture and neither the lookahead.

In the second case lookahead is applied on winnie.SO it fails.