Antoine
Antoine

Reputation: 1719

grepl in R: spurious match despite intra-word dash

below is a minimal reproducible example:

v=c("\\<skill-saw\\>","\\<saw blade\\>")
text="xx placed his hand beneath skill-saw blade"
sapply(v,grepl,text)

The last command returns c(TRUE,TRUE) where I was expecting c(TRUE,FALSE). Any idea on how to achieve that? The idea is that the keyword "skill-saw" should be detected as present in the text, but not the keyword "saw blade"...

Thanks a lot in advance for your help!

Upvotes: 0

Views: 86

Answers (1)

akrun
akrun

Reputation: 887153

You can try regex lookbehind

v <- c('(?<= )\\bskill-saw\\b', '(?<= )\\bsaw blade\\b')
 unname(sapply(v, grepl, text, perl=TRUE))
 #[1]  TRUE FALSE

Update

Based on the new "text", may be

text1 <- "xx placed his hand beneath skill saw-blade"

v <- c('(?<= )\\bskill-saw\\b', '(?<= )\\bsaw-?blade\\b')
unname(sapply(v, grepl, text1, perl=TRUE))
#[1] FALSE  TRUE

Upvotes: 1

Related Questions