Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
grep使用具有多个模式的字符向量_R_Regex - Fatal编程技术网

grep使用具有多个模式的字符向量

grep使用具有多个模式的字符向量,r,regex,R,Regex,我试图使用grep测试字符串向量是否存在于另一个向量中,并输出匹配模式中存在的值 我有这样一个数据框: FirstName Letter Alex A1 Alex A6 Alex A7 Bob A1 Chris A9 Chris A6 我有一个字符串向量模式可以在字母列中找到,例如:cA1,A9,A6 我想检查模式向量中的任何字符串是否存在于字母列中。如果是,我希望输出唯一值 问题是,我不知道如何对多个模式使用grep。我试过:

我试图使用grep测试字符串向量是否存在于另一个向量中,并输出匹配模式中存在的值

我有这样一个数据框:

FirstName Letter   
Alex      A1
Alex      A6
Alex      A7
Bob       A1
Chris     A9
Chris     A6
我有一个字符串向量模式可以在字母列中找到,例如:cA1,A9,A6

我想检查模式向量中的任何字符串是否存在于字母列中。如果是,我希望输出唯一值

问题是,我不知道如何对多个模式使用grep。我试过:

matches <- unique (
    grep("A1| A9 | A6", myfile$Letter, value=TRUE, fixed=TRUE)
)

但它给了我0个匹配,这是不正确的,有什么建议吗

我建议编写一个小脚本并使用Grep进行多个搜索。我从来没有找到一种方法来搜索多种模式,相信我,我已经找到了

与此类似,您的shell文件包含一个嵌入字符串:

 #!/bin/bash 
 grep *A6* "Alex A1 Alex A6 Alex A7 Bob A1 Chris A9 Chris A6";
 grep *A7* "Alex A1 Alex A6 Alex A7 Bob A1 Chris A9 Chris A6";
 grep *A8* "Alex A1 Alex A6 Alex A7 Bob A1 Chris A9 Chris A6";
然后输入myshell.sh运行

如果希望能够在命令行上传入字符串,请按如下方式执行,并使用shell参数this is bash notation btw:

 #!/bin/bash 
 $stingtomatch = "${1}";
 grep *A6* "${stingtomatch}";
 grep *A7* "${stingtomatch}";
 grep *A8* "${stingtomatch}";
等等


如果有很多模式需要匹配,可以将其放入for循环中

除了@Marek关于不包含fixed==TRUE的评论之外,正则表达式中也不需要空格。它应该是A1 | A9 | A6

你还提到有很多模式。假设它们在一个向量中

toMatch <- c("A1", "A9", "A6")
您是否尝试过match或charmatch函数

示例用法:

match(c("A1", "A9", "A6"), myfile$Letter)

根据Brian Digg的帖子,这里有两个过滤列表的有用函数:

#Returns all items in a list that are not contained in toMatch
#toMatch can be a single item or a list of items
exclude <- function (theList, toMatch){
  return(setdiff(theList,include(theList,toMatch)))
}

#Returns all items in a list that ARE contained in toMatch
#toMatch can be a single item or a list of items
include <- function (theList, toMatch){
  matches <- unique (grep(paste(toMatch,collapse="|"), 
                          theList, value=TRUE))
  return(matches)
}

补充布莱恩·迪格斯的答案

使用grepl的另一种方法将返回包含所有值的数据帧

toMatch <- myfile$Letter

matches <- myfile[grepl(paste(toMatch, collapse="|"), myfile$Letter), ]

matches

Letter Firstname
1     A1      Alex 
2     A6      Alex 
4     A1       Bob 
5     A9     Chris 
6     A6     Chris

也许更干净一点。。。也许吧?

不确定这个答案是否已经出现了

对于问题中的特定模式,只需一次grep调用


答案很好,但是不要忘记dplyr的过滤器:

patterns <- c("A1", "A9", "A6")
>your_df
  FirstName Letter
1      Alex     A1
2      Alex     A6
3      Alex     A7
4       Bob     A1
5     Chris     A9
6     Chris     A6

result <- filter(your_df, grepl(paste(patterns, collapse="|"), Letter))

>result
  FirstName Letter
1      Alex     A1
2      Alex     A6
3       Bob     A1
4     Chris     A9
5     Chris     A6
用机灵的


把空间拿走。我们也要这样做:

matches <- unique(grep("A1|A9|A6", myfile$Letter, value=TRUE, fixed=TRUE))
这应该起作用:

grep(pattern = 'A1|A9|A6', x = myfile$Letter)
或者更简单地说:

library(data.table)
myfile$Letter %like% 'A1|A9|A6'

谢谢你,圣诞豆。实际上,模式很多,也许使用文件会更好。我是BASH的新手,但也许像这样的东西应该有用/“pattern.txt”中i的bin/bash do echo$i j='grep-c${i}myfile.txt'echo$j如果[$j-eq o],则echo$i>>matches.txt fi donedesn不起作用…错误消息为'[grep:command not found'…我在/bin文件夹中有grep,并且/bin在我的$PATH上…不确定发生了什么事…请您提供帮助?您不能使用fixed=TRUE,因为您的模式是TRUE正则表达式。使用match或%in%或甚至==是比较精确匹配的唯一正确方法。对于此类任务,regex非常危险,可能会导致未经验证的错误ted结果。当您的字符串列表包含正则表达式运算符作为标点时,有什么方法可以做到这一点?@user1987097它应该以同样的方式工作,无论是否有任何其他正则表达式运算符。您是否有一个具体的例子,这对您不起作用?@user1987097在点或括号之前使用2个反斜杠。第一个反斜杠是解释秒的转义字符ond需要禁用运算符。使用正则表达式进行精确匹配对我来说似乎很危险,可能会产生意外的结果。为什么不在%myfile$Letter中匹配%?@user4050?没有具体原因。问题中的版本有它,我可能只是在没有考虑是否有必要的情况下执行了它。我认为grepl可以使用它当我们需要长度为1的向量时,我们有3个长度为3的向量,所以我们可以使用一些友好的grepl分隔符-|将它们与一个结合起来,试试其他的运气:哦,我现在明白了。这是一种压缩方式,可以输出类似于A1 | A2的内容,所以如果需要所有条件,则崩溃将带有&符号,比ks.Hi,使用|来分离模式可能会使它更加健壮:paste0,pastepatterns,collapse=|,不幸的是,它也变得稍微不那么优雅。这导致模式A1 | A9 | A6。%like%不在基本R中,所以您应该提到需要使用它的包。对于其他人来说,%like%是data.table包的一部分。还有在data.table中类似于…、%ilike%和%flike%。match需要注意的一点是,它没有使用模式,而是期望精确匹配。
matches <- unique(grep("A1|A9|A6", myfile$Letter, value=TRUE, fixed=TRUE))
grep(pattern = 'A1|A9|A6', x = myfile$Letter)
library(data.table)
myfile$Letter %like% 'A1|A9|A6'