问题描述
我有两个向量:
sample1 <- c(".aaa", ".aarp", ".abb", ".abbott", ".abogado")
sample2 <- c("try1.aarp", "www.tryagain.aaa", "255.255.255.255", "onemoretry.abb.abogado")
我正在尝试删除在sample2中找到的sample1字符串.我得到的最接近的是通过使用sapply
进行迭代,这给了我:
I am trying to remove sample1 strings that are found in sample2. The closest I got is by iterating using sapply
, which gave me this:
sapply(sample1, function(i)gsub(i, "", sample2))
.aaa .aarp .abb .abbott .abogado
[1,] "try1.aarp" "try1" "try1.aarp" "try1.aarp" "try1.aarp"
[2,] "www.tryagain" "www.tryagain.aaa" "www.tryagain.aaa" "www.tryagain.aaa" "www.tryagain.aaa"
[3,] "255.255.255.255" "255.255.255.255" "255.255.255.255" "255.255.255.255" "255.255.255.255"
[4,] "onemoretry.abb.abogado" "onemoretry.abb.abogado" "onemoretry.abogado" "onemoretry.abb.abogado" "onemoretry.abb"
当然预期的输出应该是
[1] "www.tryagain" "try1" "onemoretry" "255.255.255.255"
感谢您的时间.
推荐答案
尝试一下,
sample1 <- c(".aaa", ".aarp", ".abb", ".abbott", ".abogado")
sample2 <- c("try1.aarp", "www.tryagain.aaa", "255.255.255.255", "onemoretry.abb.abogado")
paste0("(",paste(sub("\\.", "\\\\.", sample1), collapse="|"),")\\b")
# [1] "(\\.aaa|\\.aarp|\\.abb|\\.abbott|\\.abogado)\\b"
gsub(paste0("(",paste(sub("\\.", "\\\\.", sample1), collapse="|"),")\\b"), "", sample2)
# [1] "try1" "www.tryagain" "255.255.255.255" "onemoretry"
说明:
-
sub("\\.", "\\\\.", sample1)
转义所有点.由于点是正则表达式中的特殊字符.
sub("\\.", "\\\\.", sample1)
escapes all the dots. Since dots are special chars in regex.
paste(sub("\\.", "\\\\.", sample1), collapse="|")
将所有元素与|
用作分隔符.
paste(sub("\\.", "\\\\.", sample1), collapse="|")
combines all the elements with |
as delimiter.
paste0("(",paste(sub("\\.", "\\\\.", sample1), collapse="|"),")\\b")
创建一个正则表达式,类似于捕获组内存在的所有元素,后跟单词边界. \\b
是这里急需的一个.这样它就可以进行精确的单词匹配.
paste0("(",paste(sub("\\.", "\\\\.", sample1), collapse="|"),")\\b")
creates a regex like all the elements present inside a capturing group followed by a word boundary. \\b
is a much needed one here . So that it would do an exact word match.
这篇关于从向量2中删除在向量1中找到的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!