无法从R中的句子中提取精确的短语

时间:2016-12-01 19:59:46

标签: r grepl

我试图从R中的句子中提取精确的短语。它还提取其部分匹配的句子。例如:

  phrase <- c("r is not working","roster is not working")
  sentence <- c("ABC is not working and roster is not working","CDE is working but printer is not working")

  extract <- sapply(phrase, grepl, x = sentence)
  extract

它将输出显示为:

              r is not working      roster is not working
  [1,]             TRUE                  TRUE
  [2,]             TRUE                 FALSE

我想要的输出是:

              r is not working      roster is not working
  [1,]               FALSE                  TRUE
  [2,]               FALSE                  FALSE

短语&#34; r不起作用&#34;不应该与两个句子匹配。有没有办法解决这个问题。有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

grepl评估正则表达式。

如果你想坚持这些,锚定你的搜索模式到字符串的开头和结尾:

phrase <- c("^r is not working$", "^roster is not working$")

如果您想要检查完全匹配,只需使用

即可
extract <- sapply(sentence, `%in%`, phrase)