简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM