gpt4 book ai didi

regex - 使用 list.files 匹配带有正则表达式的文件

转载 作者:行者123 更新时间:2023-12-04 04:33:32 24 4
gpt4 key购买 nike

我有一个 list.files 的模式,可以找到其中的一些,但是当我尝试指定更多时,我什么也没得到。

在这里,您可以看到,只需添加 [:alpha:] 之前列出的所有文件,然后我才能少一个应该出现的值(如果我对正则表达式理解得很好):

> list.files(pattern = paste( "UP.csv", sep=""), ignore.case=FALSE)
[1] "TvsPTglult-PglultUP.csv" "TvsPTglusp-PgluspUP.csv"
[3] "TvsPTglysp-PglyspUP.csv" "TvsPTmeth-PmethUP.csv"
> list.files(pattern = paste( "[:alpha:]UP.csv", sep=""), ignore.case=FALSE)
[1] "TvsPTglusp-PgluspUP.csv" "TvsPTglysp-PglyspUP.csv"
[3] "TvsPTmeth-PmethUP.csv"
#Or just without paste and just '
> list.files(pattern = '[:alpha:]UP.csv', ignore.case=FALSE)
[1] "TvsPTglusp-PgluspUP.csv" "TvsPTglysp-PglyspUP.csv"
[3] "TvsPTmeth-PmethUP.csv"
#And trying to specify with how many times it appears:
> list.files(pattern = '[:alpha:]{6,10}UP.csv', ignore.case=FALSE)
character(0)
#Adding a point to match any single character and I get just one
> list.files(pattern = '[:alpha:].UP.csv', ignore.case=FALSE, full.names=FALSE)
[1] "TvsPTglult-PglultUP.csv"

有人可以解释一下我应该如何做才能获得第一个命令中除 Pmeth 之外的所有命令吗?
但请 解释 ,我想更频繁地使用正则表达式。

编辑: session 信息
sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C

最佳答案

您需要输入 [:alpha:]在一组额外的方括号内:

ff <- c("TvsPTglult-PglultUP.csv","TvsPTglusp-PgluspUP.csv",
"TvsPTglysp-PglyspUP.csv","TvsPTmeth-PmethUP.csv")

grep("[[:alpha:]]{6,10}UP.csv",ff) ## 1 2 3

关于regex - 使用 list.files 匹配带有正则表达式的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20146375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com