简体   繁体   中英

Exact string matching in r

I struggling with exact string matching in R. I need only exact match in sentece with searched string:

sentence2 <- "laptop is a great product"
words2 <- c("top","laptop")

I was trying something like this:

sub(paste(c("^",words2,"$")),"",sentence2)

and I need replace laptop by empty string only - for exact match (laptop) but didn't work...

Please, could you help me. Thanks in advance.

Desired output:

is a great product

You can try:

gsub(paste0("^",words2," ",collapse="|"),"",sentence2)
#[1] "is a great product"

The result of paste0("^",words2," ",collapse="|") is "^top |^laptop " which means "either 'top' at the beginning of string followed by a space or 'laptop' at the beginning of string followed by a space".

If you want to match entire words, then you can use \\\\b to match word boundaries.

gsub(paste0('\\b', words2, '\\b', collapse='|'), '', sentence2)

## [1] " is a great product"

Add optional whitespace to the pattern if you want to replace the adjacent spaces as well.

gsub(paste0('\\s*\\b', words2, '\\b\\s*', collapse='|'), '', sentence2)

## [1] "is a great product"

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