Filter a vector of strings based on string matching

后端 未结 4 1184
[愿得一人]
[愿得一人] 2020-12-30 19:43

I have the following vector:

X <- c(\"mama.log\", \"papa.log\", \"mimo.png\", \"mentor.log\")

How do I retrieve another vector that only

4条回答
  •  太阳男子
    2020-12-30 20:30

    Try this:

    grep("^m.*[.]log$", X, value = TRUE)
    ## [1] "mama.log"   "mentor.log"
    

    A variation of this is to use a glob rather than a regular expression:

    grep(glob2rx("m*.log"), X, value = TRUE)
    ## [1] "mama.log"   "mentor.log"
    

提交回复
热议问题