简体   繁体   中英

Not able to extract exact phrase from the sentence in R

I am trying to extract exact phrase from the sentences in R. It is also extracting the sentence where its is partial matching. Example:

  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

It gives the output as:

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

My desired output is:

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

phrase "r is not working" should not match with both sentences. Is there any way to deal with this. Any thoughts? Thanks!!

grepl evaluates regular expressions.

If you want to stick with those, anchor your search patterns to the start and end of string:

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

If you instead want to check for exact matches, simply use

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

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