根据字符串匹配过滤字符串向量 [英] Filter a vector of strings based on string matching
本文介绍了根据字符串匹配过滤字符串向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下向量:
X <- c("mama.log", "papa.log", "mimo.png", "mentor.log")
如何检索仅包含以"m"开头和以".log"结尾的元素的另一个向量?
How do I retrieve another vector that only contains elements starting with "m" and ending with ".log"?
推荐答案
您可以将grepl
与正则表达式配合使用:
you can use grepl
with regular expression:
X[grepl("^m.*\\.log", X)]
这篇关于根据字符串匹配过滤字符串向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文