regex - Lookahead Behaviour -
how can make lookahead non-greedy? first case not match (like second case), returns "winnie". guess because greedily matching after "the"?
str <- "winnie 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 being applied bear in winnie|bear(?= bear|pooh) , not winnie.if want apply on both use
(?:winnie|bear)(?= bear|pooh) now apply on both. because winnie matched ored part bear never came picture , neither lookahead.
in second case lookahead applied on winnie.so fails.
Comments
Post a Comment