精确的单词匹配,而不是字符串之间的匹配

时间:2019-05-28 09:07:10

标签: r regex

下面的代码不是整体匹配,而是返回两者之间存在匹配字符的情况。

for(i in 1:dim(Step10)[1]) {
x <- agrep(Step10$Supplier.Name[i], Step9$SUPPLIER_NAME,
         ignore.case=TRUE, value=FALSE, fixed = TRUE,
         max.distance = 0.05, useBytes = FALSE)
x <- paste0(x,"")
Step10$Supplier_Name[i] <- x
}

下面是代码

a<- c("LEARNING LLC", "BOC", "NCC","KAMAY")
b<- c("LEARNING LLC","BOCLKYN CENTRE", "YANCO CORP", "SAMAS DBA KAMAY")

a<- as.data.frame(a)
b<- as.data.frame(b)

colnames(a) <- c("names")
colnames(b) <- c("names")

a$names = as.character(a$names)
b$names = as.character(b$names)

a$name_1 <- ""

for(i in 1:dim(a)[1]) {
  x <- agrep(a$names[i],b$names,
         ignore.case=TRUE, value=TRUE,
         max.distance = 0.05, useBytes = TRUE)
  x <- paste0(x,"")
  a$name_1[i] <- x
  }

1 个答案:

答案 0 :(得分:0)

由于您需要完全匹配,因此必须使用grep并在单词周围添加单词边界,即

grep(paste0('\\b', a, '\\b', collapse = '|'), b, value = TRUE)
#[1] "LEARNING LLC"    "SAMAS DBA KAMAY"