2016-12-01 92 views
0

我試圖從R中的句子中提取精確的短語。它也提取了其部分匹配的句子。例如:無法從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 

短語 「R不工作」 不應該匹配兩個句子。有什麼辦法可以解決這個問題嗎?有什麼想法嗎?謝謝!!

+0

可能會添加字邊界,如'sapply(paste0(「\\ b」,短語,「\\ b」),grepl,x =句子)' –

+0

「r不工作」匹配兩個字符串,但添加一個空格在r:「r不工作」之前將阻止匹配。 – Dave2e

回答

1

grepl評估正則表達式。

如果你想堅持的,您的搜索模式以字符串的開始和結束:

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

如果你不是要檢查精確匹配,簡單地使用

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