Regex: "(^ |)" vs "(| ^)"

I have a special question about regular expressions in R:

grepl("(|^)over","stackoverflow") # [1] TRUE grepl("(^|)over","stackoverflow") # [1] FALSE grepl("(^|x|)over","stackoverflow") # [1] FALSE grepl("(x|^|)over","stackoverflow") # [1] FALSE grepl("(x||^)over","stackoverflow") # [1] TRUE 

Why aren't all of these expressions evaluated before TRUE ?

+7
regex r
source share
1 answer

POSIX regular expressions should actually make all of these True. It appears that R is using a slightly modified version of the Ville Laurikari TRE library , which is not quite up to standard. I will follow @rawr's guidelines and use perl = TRUE for more compatible regular expressions.

See also: If both halves of an OR regular expression group match, will it be selected?

+6
source share

All Articles