如何grep匹配R中两个(或多个)变量的字符串

时间:2019-04-17 13:13:31

标签: r regex list grep

我有一个列表,我想从中提取特定元素以放入新列表中,例如

   MyFullList = (
  "
  'BLUEh473vs65hRED7vfd8edgf9GLOSS.csv', 
  'BLUE5654vsy56hgfREDjkgr7MATTE.csv', 
  'BLUEhjirhgccsREDgfigjINCAN.csv', 
  'BLUEvfdjhkldfgsGREENdfggh5GLOSS.csv', 
  'BLUEtgergvsGREENsghjkhgnMATTE.csv',
  'BLUEjgh4gvsGREENjkhgfdINCAN.csv',

  ....

  'GREENhfuewhvsBLUEhfsMATTE.csv'
  'GREENkdfgvsBLUEfjhINCAN.csv'
"

)

如果我只想提取以Blue开头但又包含Green和Matte的文件,我该怎么做?

目前,我已经设法适应了其他人的要求,但是我无法对其进行进一步的操作:

MySelection <- MyFullList[grep("^(?=[^BLUE])(?=.*MATTE)", names(MyFullList), value = T, perl=T)]

似乎只是返回任何包含遮罩的内容。

我也尝试过诸如:

MySelection <- MyFullList[grep("^BLUE|GREEN|MATTE.csv$"), names(MyFullList), value = T]

我认为这具有相同的效果。

我尝试使用星号:

MySelection <- MyFullList[grep("^BLUE.*GREEN.*MATTE.csv$"), names(MyFullList), value = T]

似乎也完全被误导了。

接着,我该如何在Grep命令中使用变量?

例如

Colours=('BLUE', 'RED', 'GREEN')

for i in 1:length(Colours) {

    grep("^[[Colours[i]]].*GREEN.*MATTE.csv$"), names(MyFullList), value = T]
}

2 个答案:

答案 0 :(得分:2)

这是由于\n而引起的问题。另一种选择是先使用read.table读取数据:

MyFullList = read.table(text=
  "
  'BLUEh473vs65hRED7vfd8edgf9GLOSS.csv', 
  'BLUE5654vsy56hgfREDjkgr7MATTE.csv', 
  'BLUEhjirhgccsREDgfigjINCAN.csv', 
  'BLUEvfdjhkldfgsGREENdfggh5GLOSS.csv', 
  'BLUEtgergvsGREENsghjkhgnMATTE.csv',
  'BLUEjgh4gvsGREENjkhgfdINCAN.csv',

  ....

  'GREENhfuewhvsBLUEhfsMATTE.csv'
  'GREENkdfgvsBLUEfjhINCAN.csv'
"

,as.is=T,header=F,fill=T)
mfl<-MyFullList[,-ncol(MyFullList)]

然后我们的结果:

mfl[grep("^BLUE.*GREEN.*MATTE.csv$",mfl)]
[1] "BLUEtgergvsGREENsghjkhgnMATTE.csv"

答案 1 :(得分:1)

您可以只使用以下三种逻辑条件:

x <- c("BLUEh473vs65hRED7vfd8edgf9GLOSS.csv", 
       "BLUE5654vsy56hgfREDjkgr7MATTE.csv",
       "BLUEhjirhgccsREDgfigjINCAN.csv",
       "BLUEvfdjhkldfgsGREENdfggh5GLOSS.csv",
       "BLUEtgergvsGREENsghjkhgnMATTE.csv",
       "BLUEjgh4gvsGREENjkhgfdINCAN.csv",
       "GREENhfuewhvsBLUEhfsMATTE.cs",
       "GREENkdfgvsBLUEfjhINCAN.csv")


x[grepl("^BLUE", x) & grepl("MATTE",x) & grepl("GREEN",x)]
[1] "BLUEtgergvsGREENsghjkhgnMATTE.csv"